Async vs Isolates | Decoding Flutter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I wish they would make async the same as Kotlin and just make it a coroutine.

👍︎︎ 2 👤︎︎ u/fontos 📅︎︎ Jun 08 2021 🗫︎ replies
Captions
[MUSIC PLAYING] FILIP HRACEK: Sometimes there's confusion between the ideas of async and isolates, concurrency and parallelism. Let's clear that up. A code like this is asynchronous. It runs normally, and then it waits while a file is being downloaded. While it waits, other parts of our program can run. Notice how there's always only one part of the app running at any given time. After all, we're running on only one thread, one processor. Async doesn't automatically make your app multithreaded. After the file is downloaded, the method will continue. That's async. The processor doesn't need to stop and do nothing until the download is finished. It makes a mental note to get back to that particular method, basically a callback. And when that future completes, it goes back there. Most of the time, async is all you need. But watch what happen if the method does some heavy computation. Let's say the file is huge, and parsing it requires a lot of processor power. Will this make our app stutter? You might think, no, it won't. The computation happens in an async method, right? But unfortunately, you're wrong. The app will stutter. Because even though the method is asynchronous, a large chunk of synchronous work is happening inside it. That parsing is synchronous. It's happening on your thread. That will keep the processor busy, and so no other part of your app will be running. The UI won't refresh until the heavy work is finished, and the user will see stutter. What you want in this case is parallelism. Dart allows you to run code in parallel via isolates. With an isolate, you start another Dart program, basically, on a different thread, on a different processor. The two separate isolates are isolated. One will not block the other. This way, your app can do heavy processing while also running smoothly for the user. I won't get into how to implement isolates here. It's a bit more involved, and this video is only meant to distinguish between the two concepts of async versus isolates. Speaking of which, let's summarize. Async is the ability to wait for other things without blocking. Async will not create a separate thread for you. Dart will just jump between the different parts of your program as needed. In most cases, that's fine. It's like if I talk to you and also sometimes check my phone. When I'm checking my phone quickly, it's fine-- kind of annoying, but fine. Right? Our conversation still flows. But if I suddenly start staring at the screen and ignoring you-- [CRICKETS CHIRPING] --then that's not great. That's async. Isolates give you the ability to run things in parallel. One isolate doesn't care what the other one is doing at any given time. They can both run at full speed. Isolates are harder to set up, but it's also the only way to deal with some situations, like the parsing example I mentioned. Isolates are like when I talk to you and also play around with Dash here. I can do those things in parallel. All right, I hope that was helpful. [MUSIC PLAYING]
Info
Channel: Flutter
Views: 57,954
Rating: undefined out of 5
Keywords: GDS: Yes, Async, Isolates, Async vs Isolates, new developer show, developer shows, developer tutorials, Flutter, Flutter tutorials, Flutter developers, flutter app development, Flutter course, how Flutter works, Flutter best practices, Decoding Flutter, Google developers, flutter 101, get started with flutter, flutter apps, flutter widgets, build apps with flutter
Id: 5AxWC49ZMzs
Channel Id: undefined
Length: 4min 24sec (264 seconds)
Published: Tue Jun 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.