7

Executing Heavy Tasks Without Blocking the Main Thread on Flutter

 3 years ago
source link: https://hackernoon.com/executing-heavy-tasks-without-blocking-the-main-thread-on-flutter-6mx31lh?source=rss
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Executing Heavy Tasks Without Blocking the Main Thread on Flutter

MJpFVUEItkSdoh38rYo60VT7RfH3-fhk285b.jpeg

@altynbergAltynbek Usenbekov

Software Developer, Solopreneur

On Flutter everything runs on a single (main) isolate and executes only one operation at a time. This means while your app is doing strenuous work, you will lose the interaction and the UI will freeze. We can create new isolates and do the heavy work there, but every isolate can reach only its own memory, and if you have to use a large set of data it won't be very optimal because we have to copy the large data from one isolate to another, and it can still block the main isolate while doing that.

Every situation requires a different approach but in most cases we can solve them on the main isolate using an event queue optimally. Let me show you one of these approaches.

In the example above, the application will be blocked during the execution. I created a simple animation to illustrate it. Every time we tap on the floating button, it executes the heavy work and the app freezes:

uwos0chJX2fO3WCG9AogsFGrN4S2-6aa3ei8.gif

To execute it on the main isolate without blocking the application we have to understand how the event queue works. Actually, it is very simple; every event will be placed into a queue and it will wait until the events before it are executed. Our method takes a long time to execute, so the events after it, like drawing the screen, will wait for us and that's why animation is not smooth. To make it smooth, we can simply divide our big piece of work into small chunks and give the opportunity for other events to execute between our chunks.

We divided our work into many small chunks and the next chunk will be added to the event queue when the current chunk finishes the execution. So the other events can be executed between our chunks. We get smooth animation as a result:

uwos0chJX2fO3WCG9AogsFGrN4S2-mxd3eh1.gif

As I said, every situation may require a different approach. But as we see, we can get a good result even when using a single thread.

I used this solution in my last project because I could not find a better way. Although it worked well for me, I felt like there must be a simple and better solution. So it would make me happy if you could show me a better solution or my mistakes.

You can find the code here

heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
Share this story

@altynbergAltynbek Usenbekov

Read my stories

Software Developer, Solopreneur

Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK