flutter_isolate icon indicating copy to clipboard operation
flutter_isolate copied to clipboard

Restore multiple instances of flutter_isolate plugin

Open ened opened this issue 3 years ago • 4 comments

Our project already runs multiple isolates in the background (driven by native components, like BT connection or Android lifecycle). These components are long-lived and need to co-exist with the UI as well.

We found that on iOS, only one of the sides (BG or FG) will be able to control isolates using flutter_isolate. This is most likely happening due to the plugin having a static instance and not initializing the control channel anymore when the instance already exists.

This MR improves the behavior and should unblock us from using it.

ened avatar Nov 29 '21 10:11 ened

@ened can you provide a bit more detail about your use case? The current assumption is that registerWithRegistrar is called once (and only once) in the app lifecycle, but it sounds like this assumption isn't appropriate for your design. It would be helpful for me to understand how you're working with isolates in both the foreground/background.

Also, have you checked whether registering a subsequent method channel with the same name doesn't interfere the first?

nmfisher avatar Dec 22 '21 15:12 nmfisher

@nmfisher whenever a Flutter Isolate is started, it could belong to a full Flutter Engine including plugins. This is the case for example in the workmanager plugin. Over there, the Android system allocates resources for an App to run in the background, which ends up being managed by workmanager, and in order to run Dart code, it starts a full Flutter Engine, including plugins. This can even happen when the App is already in foreground. Similarly, for push notifications using firebase_messaging (when receiving a notification while App is dead).

In our use case, we had a long-running BT connection (and associated FG service with a Flutter Engine) which provides context to run Dart code long term and both the FG and BG sides wanted to execute additional isolates using this flutter_isolate plugin. It was prevented by the assumption that registerWithRegistrar is only called once, as it's actually being called "only once per Flutter Engine".

ened avatar Jan 07 '22 06:01 ened

Thanks @ened - I think I understand your issue now:

  1. you're working with isolate(s) spawned by flutter_isolate, and isolate(s) spawned by separate Flutter Engine(s)/plugin(s),
  2. plugins created in the former won't be able to communicate with control channels created in the latter (and vice versa).

Is everything working correctly on Android?

nmfisher avatar Jan 07 '22 07:01 nmfisher

Thanks @ened - I think I understand your issue now:

  1. you're working with isolate(s) spawned by flutter_isolate, and isolate(s) spawned by separate Flutter Engine(s)/plugin(s),

  2. plugins created in the former won't be able to communicate with control channels created in the latter (and vice versa).

Is everything working correctly on Android?

Yes we shipped production apps with the fork without issues.

ened avatar Jan 07 '22 08:01 ened

This PR and https://github.com/rmawatson/flutter_isolate/pull/118 should be merged to master ASAP, they unbreak this plugin for a lot of functionality.

srmncnk avatar Sep 07 '22 10:09 srmncnk

Thanks @ened

nmfisher avatar Sep 16 '22 23:09 nmfisher