Getting back isolateID
Hi,
is there a way to getting back the isolateId from the plugin ? Maybe you can return a Future with the uuid on the spawn method , or making public the field _isolateId.
thanks
Marco
I agree, we need isolateId so we can kill an isolate after use of it. I wonder when they can make that available. I'm looking at the FlutterIsolate dart source code to see if it's something I can do myself.
As long as you keep a reference to the spawned isolate, you can just invoke kill (the README/example project contains example code).
I'm not really sure why you would need the isolate ID?
@nmfisher It would be useful if the main activity was destroyed before the isolate completed it's task. Storing the id in local could help in killing it after coming back to the app.
@indrajit-roy I thought the whole Flutter lifecycle was intrinsically linked to the main activity, so when that's destroyed all the executors are destroyed too. Can you point me to some example Flutter code where the main activity is destroyed but the isolate is still running?
Unless you're talking about launching an isolate in a background service, which I'm not even sure is possible.
@nmfisher you can take a look at this. https://medium.com/@lelandzach/dart-isolate-2-way-communication-89e75d973f34#:~:text=The%20Dart%20programming%20language%20allows,to%20execute%20the%20proceeding%20code.
Its a basic isolate spawn. Although on pressing the back button and exiting the app, the isolate runs in the background if not killed. This is proved by the debug console if I start a timer inside the isolate to print every tick. In the app life cycle observer the, the detached callback is executed(which should mean onDestroy() for the MainActivity?). So when I come back to the app, the main isolate is executed from the start and I lose all references that I had earlier.
Thanks @indrajit-roy - I just verified that the spawned isolate will continue to run after the FlutterActivity is destroyed (and the app is in AppLifecycleState.paused).
Assuming you were given the isolate ID at spawn time, how would you propose to store/retrieve this after the app is paused/resumed? Presumably you would need to make this persistent somewhere?
@nmfisher yes. The most obvious solution would be to cache the returned Info about the Isolate using solutions like shared_preferences / hive etc.
I wonder if there's a better way to do it. Like a way to retrieve the current running isolates and their info at any given point of time. That would solve the ambiguity of whether a stored isolate id is still running, before we kill it. That would actually take away the necessity of caching the isolate info at all. I don't know if there is a way to broadcast a SendPort to all running isolates so they can send back this info, maybe through a default behavior implementation of a receive port which would receive this broadcast. That would be super useful.
Looks like the main Dart executor is killed when the activity is destroyed (and possibly the native plugin instance, I'd need to check), but if we make the native isolate registry static then it should be straightforward to add a getActiveIsolateIds() method.