Replies: 2 comments 5 replies
|
Hey, good catch — this is a real gap, not you missing something. Short version: cancelling a task genuinely does NOT stop a running DartWorker callback right now. Just pushed a fix for it, answering your questions in order:
'longSync': (input) async {
final taskId = input?['__taskId'] as String?;
for (var i = 1; i <= 100; i++) {
if (taskId != null && await NativeWorkManager.isTaskCancelled(taskId)) {
return false; // bail, dont keep working
}
await processChunk(i);
}
return true;
},If your op is one big blocking call with no yield points this won't help much — would need to chunk it.
Also found 2 related bugs digging into this — iOS wasn't even cancelling the actual background task on BGTask expiration (just a no-op stop() call), and Android could leak the dart engine on cancel. Both fixed same PR. Will be in the next release. Let me know if the polling shape is awkward for what you're doing and I'll think about alternatives. |
|
quick update — v1.8.0 just shipped (live on pub.dev now). isTaskCancelled actually works now on Android too. Turned out the first pass I mentioned above had a bug of its own: the cancellation registry cleared its own entry the instant it was set, so on Android the whole thing was silently a no-op — unit tests stayed green because a mocked channel can't reproduce that timing race, only caught it once i got a real device to test on. Fixed and device-verified on a Pixel 6 Pro + iOS sim + a 3-device Firebase Test Lab run this time, not just unit tests. polling shape is still the same as the snippet above. lmk if it's awkward for your use case. |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I’m using native_workmanager in my Flutter application and I’m trying to understand how task cancellation works.
When I cancel a task using WorkManager, the native WorkManager task appears to be cancelled, but the Dart code that is already running continues to execute.
For example, if my task is performing a long-running operation:
await longRunningOperation();and I cancel the WorkManager task while this operation is running, the Dart code doesn’t seem to stop immediately.
Is there a recommended way to:
I’m particularly interested in knowing whether there is an API such as onTaskStopped or a cancellation token that I should use.
Thanks!
All reactions