Norbi
06/07/2024, 4:47 PMCorouteScopes.
Then I launch a coroutine in one of these scopes.
How to make this launched coroutine to be automatically cancelled if any of the two scopes is cancelled?
Thanks.Joffrey
06/07/2024, 4:49 PMinvokeOnCompletion callback on its Job, and cancel your own coroutine from it manually if it's a cancellation.Sam
06/07/2024, 4:49 PMscope2.launch(start = ATOMIC) {
try {
awaitCancellation()
} finally {
jobFromScope1.cancel()
}
}Joffrey
06/07/2024, 4:51 PMval jobFromScope1 = scope1.launch { ... }
scope2.job.invokeOnCompletion { cause ->
if (cause != null) {
jobFromScope1.cancel()
}
}Awkin
06/07/2024, 4:52 PMkevin.cianfarini
06/07/2024, 4:52 PMSam
06/07/2024, 4:52 PMscope2 never terminates normally, only via cancellation/failure. Thatโs normally the case for custom scopes, but not for e.g. the coroutineScope functionNorbi
06/07/2024, 4:56 PM