5

I have a separate isolate which listens to a cloud-firestore document via stream snapshot.
For this, I use isolate_handler package which works well.
So to use cloud-firestore inside the isolate, await Firebase.initializeApp() is required which creates a new Firebase App instance.

The only problem arises when killing off the isolate by calling IsolateHandler.kill(name);
Looks like the whole Firebase instance is killed off from the native side as well when the isolate is killed.

Here's a small representation of the implementation:

static launchStreamIsolate(Function(IsolatedQuerySnapshot) snapshot) {
    if (!_isolateHandler.isolates.containsKey("listener"))
      /// _startStream is a function that listens to the changes on Firestore.
      _isolateHandler.spawn(_startStream,
          name: "stream",
          onInitialized: () => _isolateHandler.send("startListening", to: "stream"),
          onReceive: (json) {
            _isolateHandler.send("stopListening", to: "stream");
            snapshot.call(IsolatedQuerySnapshot.fromJson(json));
          });
  }

This works fine but when I call the following method in the Widget's dispose() callback, the app crashes:

static disposeOff() async {
    _isolateHandler.isolates.forEach((key, value) => _isolateHandler.kill(key));
    print("Secondary isolates disposed");
  }

The stacktrace:

-[__NSCFString setStreamHandler:]: unrecognized selector sent to instance 0x281207100
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setStreamHandler:]: unrecognized selector sent to instance 0x281207100'
*** First throw call stack:
(0x19a58986c 0x1af4f8c50 0x19a49095c 0x19a58c438 0x19a58e740 0x104b188e4 0x104b1923c 0x19a4efadc 0x19a466280 0x105c182cc 0x10529db6c 0x1af4f7b10 0x1af50e840 0x1af51580c 0x10529e68c 0x1064a4694 0x105c44038 0x105f4341c 0x105ee281c 0x105ee4ed4 0x19a505fa0 0x19a505ba0 0x19a504ffc 0x19a4feee4 0x19a4fe21c 0x1b2002784 0x19cf3cfe0 0x19cf42854 0x1049082e4 0x19a1be6b0)
libc++abi.dylib: terminating with uncaught exception of type NSException

The setStreamHandler comes from the native side of the cloud-firestore implementation.
How can this be fixed or is there something wrong with my implementation here?

Update: There seems to be a similar issue here which also has my hacky-fix to this.

Darshan
  • 4,020
  • 2
  • 18
  • 49

0 Answers0