text stringlengths 1 474 |
|---|
On the main isolate, |
these events can be anything from handling a user tapping in the UI, |
to executing a function, |
to painting a frame on the screen. |
The following figure shows an example event queue |
with 3 events waiting to be processed.For smooth rendering, |
Flutter adds a “paint frame” event to the event queue |
60 times per second(for a 60Hz device). |
If these events aren’t processed on time, |
the application experiences UI jank, |
or worse, |
become unresponsive altogether.Whenever a process can’t be completed in a frame gap, |
the time between two frames, |
it’s a good idea to offload the work to another isolate |
to ensure that the main isolate can produce 60 frames per second. |
When you spawn an isolate in Dart, |
it can process the work concurrently with the main isolate, |
without blocking it.You can read more about how isolates |
and the event loop work in Dart on |
the concurrency page of the Dart |
documentation.<topic_end> |
<topic_start> |
Common use cases for isolates |
There is only one hard rule for when you should use isolates, |
and that’s when large computations are causing your Flutter application |
to experience UI jank. |
This jank happens when there is any computation that takes longer than |
Flutter’s frame gap.Any process could take longer to complete, |
depending on the implementation |
and the input data, |
making it impossible to create an exhaustive list of |
when you need to consider using isolates.That said, isolates are commonly used for the following:<topic_end> |
<topic_start> |
Message passing between isolates |
Dart’s isolates are an implementation of the Actor model. |
They can only communicate with each other by message passing, |
which is done with Port objects. |
When messages are “passed” between each other, |
they are generally copied from the sending isolate to the |
receiving isolate. |
This means that any value passed to an isolate, |
even if mutated on that isolate, |
doesn’t change the value on the original isolate.The only objects that aren’t copied when passed to an isolate |
are immutable objects that can’t be changed anyway, |
such a String or an unmodifiable byte. |
When you pass an immutable object between isolates, |
a reference to that object is sent across the port, |
rather than the object being copied, |
for better performance. |
Because immutable objects can’t be updated, |
this effectively retains the actor model behavior.An exception to this rule is |
when an isolate exits when it sends a message using the Isolate.exit method. |
Because the sending isolate won’t exist after sending the message, |
it can pass ownership of the message from one isolate to the other, |
ensuring that only one isolate can access the message.The two lowest-level primitives that send messages are SendPort.send, |
which makes a copy of a mutable message as it sends, |
and Isolate.exit, |
which sends the reference to the message. |
Both Isolate.run and compute |
use Isolate.exit under the hood.<topic_end> |
<topic_start> |
Short-lived isolates |
The easiest way to move a process to an isolate in Flutter is with |
the Isolate.run method. |
This method spawns an isolate, |
passes a callback to the spawned isolate to start some computation, |
returns a value from the computation, |
and then shuts the isolate down when the computation is complete. |
This all happens concurrently with the main isolate, |
and doesn’t block it.The Isolate.run method requires a single argument, |
a callback function, |
that is run on the new isolate. |
This callback’s function signature must have exactly |
one required, unnamed argument. |
When the computation completes, |
it returns the callback’s value back to the main isolate, |
and exits the spawned isolate.For example, |
consider this code that loads a large JSON blob from a file, |
and converts that JSON into custom Dart objects. |
If the json decoding process wasn’t off loaded to a new isolate, |
this method would cause the UI to |
become unresponsive for several seconds. |
<code_start>// Produces a list of 211,640 photo objects. |
// (The JSON file is ~20MB.) |
Future<List<Photo>> getPhotos() async { |
final String jsonString = await rootBundle.loadString('assets/photos.json'); |
final List<Photo> photos = await Isolate.run<List<Photo>>(() { |
final List<Object?> photoData = jsonDecode(jsonString) as List<Object?>; |
return photoData.cast<Map<String, Object?>>().map(Photo.fromJson).toList(); |
}); |
return photos; |
}<code_end> |
For a complete walkthrough of using Isolates to |
parse JSON in the background, see this cookbook recipe.<topic_end> |
<topic_start> |
Stateful, longer-lived isolates |
Short-live isolates are convenient to use, |
but there is performance overhead required to spawn new isolates, |
and to copy objects from one isolate to another. |
If you’re doing the same computation using Isolate.run repeatedly, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.