【发布时间】:2020-03-06 09:34:35
【问题描述】:
我知道compute function 基于Isolate API。对于 Isolate,您可以通过调用 islate 对象的kill 方法请求关闭 lsolate。
compute 函数能否手动关闭运行此任务的 Isolate?
【问题讨论】:
-
不,你不能使用
compute()顶级函数来做到这一点
标签: flutter dart dart-isolates
我知道compute function 基于Isolate API。对于 Isolate,您可以通过调用 islate 对象的kill 方法请求关闭 lsolate。
compute 函数能否手动关闭运行此任务的 Isolate?
【问题讨论】:
compute()顶级函数来做到这一点
标签: flutter dart dart-isolates
不可能杀死compute function。
原因是看the source code of the compute function,创建的isolate只有在result完成后才被杀死:
final Completer<R> result = Completer<R>();
...
await result.future;
...
isolate.kill(); // Always awaits the result.
仅当出现错误或传递给compute 的函数返回时,结果才会完成。
此外,您确实没有有权访问isolate 您自己,因为它是created inside of the compute function。
如果您希望能够杀死您启动的隔离,请不要使用compute。相反,您必须自己创建Isolate。
【讨论】: