https://kotlinlang.org logo
#coroutines
Title
# coroutines
z

Zoltan Demant

08/20/2022, 6:13 AM
I want to cancel all the pending work thats being done in my
CoroutineScope
, but I need to do it inside a suspending block of my scope. This feels like a really bad idea, is there a better approach?
Copy code
scope.launch {
    // Do some suspending work
    scope.coroutineContext.cancelChildren()
    // Ordering becomes very important - any code from here and on is never executed.
    // This isnt a problem by itself as I dont have any more code here, but it has introduced very subtle
    // bugs in the past
}
For context: My use-case is pretty simple. You sign out from some service, and the app cancels any pending work thats being done. Since you can sign in anytime again, I keep the same scope around.
5 Views