Firestore 侦听器位于单独的 Isolate 中

时间:2021-05-17 07:47:32

标签: flutter dart-isolates

我有一个单独的 isolate,它通过流快照侦听 cloud-firestore 文档。
为此,我使用了运行良好的 isolate_handler 包。
因此,要在隔离中使用 cloud-firestore,需要 await Firebase.initializeApp() 来创建新的 Firebase 应用实例。

通过调用 IsolateHandler.kill(name);
杀死隔离时出现的唯一问题 当隔离被杀死时,整个 Firebase 实例似乎也从本机端被杀死。

这是实现的一个小表示:

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));
          });
  }

这很好用,但是当我在 Widget 的 dispose() 回调中调用以下方法时,应用程序崩溃了:

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

堆栈跟踪:

-[__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

setStreamHandler 来自 cloud-firestore 实施的本机方面。
如何解决这个问题,或者我在这里的实现有什么问题?

更新:似乎有一个类似的问题 here 这也有我的黑客修复。

0 个答案:

没有答案
相关问题