flutter_isolate icon indicating copy to clipboard operation
flutter_isolate copied to clipboard

Getting back isolateID

Open freedreamer82 opened this issue 5 years ago • 8 comments

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

freedreamer82 avatar May 03 '20 20:05 freedreamer82

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.

AxesandGrinds avatar Jun 18 '20 15:06 AxesandGrinds

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 avatar Jun 28 '21 04:06 nmfisher

@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 avatar Aug 05 '21 19:08 indrajit-roy

@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 avatar Aug 18 '21 15:08 nmfisher

@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.

indrajit-roy avatar Aug 25 '21 17:08 indrajit-roy

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 avatar Sep 01 '21 05:09 nmfisher

@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.

indrajit-roy avatar Sep 02 '21 19:09 indrajit-roy

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.

nmfisher avatar Sep 04 '21 05:09 nmfisher