Dart Isolates accessing a shared object instance

Issue

I’m currently working with isolates to send queries in parallel to a database server. I have a connector-object to build the connection to the database and I would like to share that across all isolates, so I don’t have to create a separate connection for every isolate.

So far it seems that I can only share special serializable objects between isolates. I’m using the send- and receive-ports for messaging. For other objects (such as my connector-object) the dart-vm yields the error:

Illegal argument(s): Illegal argument in isolate message : (object extends NativeWrapper)

Do you know of any way to share a generic object-instance between multiple isolates? Or do I have to create a separate instance for each isolate?

Thanks!

Pedro

Solution

from the SendPort.send method code doc

   * In the special circumstances when two isolates share the same code and are
   * running in the same process (e.g. isolates created via [Isolate.spawn]), it
   * is also possible to send object instances (which would be copied in the
   * process). This is currently only supported by the dartvm.  For now, the
   * dart2js compiler only supports the restricted messages described above.

I assume that objects that are wrappers of native objects aren’t supported either.
It seems currently the only way is to create a new connection in each isolate.

Always working are these kinds of values

   * The content of [message] can be: primitive values (null, num, bool, double,
   * String), instances of [SendPort], and lists and maps whose elements are any
   * of these. List and maps are also allowed to be cyclic.

Answered By – Günter Zöchbauer

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *