Isolate.spawn(). Is it possible to pass a class by reference to an isolate? I did read that it is possible, but I could find no example. Even though the Isolate has it’s own memory space, I don’t see why it should not be possible to pass by reference. What I want to do is pass a class by reference to the isolate and for the isolate to update a member of that class and then return a response with the data being in the Class. It appears however from my testing that the Isolate simply copies the class and then updates its copy with the return value. Obviously if this were possible (by reference) it would be more efficient for large data items. If possible, an example would be good. If not possible, some information as to the reason would be helpful.
What prompted this question is the problem that I have whereby I am creating about 10 Bar-Charts using Widgets as opposed to painting them. It takes roughly 2.5 seconds to create and then display the Bar-Charts on a not-too-fast Android phone. I previously created Pie Charts for the same data and that is much faster. So, I wanted to display a CircularProgressIndicator or similar while the Bar-Charts are being constructed. Surprisingly to me, there does not appear to be an easy way to do this. This led me to investigate the use of isolates. I have used Isolates previously for the download of a large number of items from the net using multiple Isolates. This worked quite well. This case however, it appears much more complex to achieve with Isolates. One factor being that an Isolate cannot call an external Function. Isolates have a very restrictive model by design, apparently to keep them relatively simple compared to the alternative. So, I will attempt to find another alternative to solve my problem – just so that I can display a CircularProgressIndicator. Perhaps it is possible to do that using a Timer.
From the Isolate documentation: “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.”
UPDATE:
I did implement the Timer and the CircularProgressIndicator which although it did work as it should, it did not solve the problem because the time was not spent building the widgets, but displaying them, by which time the updates to the CircularProgressIndicator had completed. I solved this by adding the Bar-Charts one-by one and after adding each one updating the State of the Widget that displays them. Now there is no noticeable delay. So, an Isolate was not the solution (too complex or impossible or would not have solved the problem) and a Timer was not the solution even though it worked as intended, because the delay was in the display of the Widget or so it seems.