text
stringlengths
1
372
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,
you might have better performance by creating isolates that don’t exit immediately.
to do this, you can use a handful of lower-level isolate-related APIs that
isolate.run abstracts:
when you use the isolate.run method,
the new isolate immediately shuts down after it
returns a single message to the main isolate.
sometimes, you’ll need isolates that are long lived,
and can pass multiple messages to each other over time.
in dart, you can accomplish this with the isolate API
and ports.
these long-lived isolates are colloquially known as background workers.
long-lived isolates are useful when you have a specific process that either
needs to be run repeatedly throughout the lifetime of your application,
or if you have a process that runs over a period of time
and needs to yield multiple return values to the main isolate.
or, you might use worker_manager to manage long-lived isolates.
<topic_end>