CLOVIS
01/11/2023, 3:39 PMscope.onCancellation {
disposeSomeStuff()
}
Sam
01/11/2023, 3:41 PMscope.launch {
try {
awaitCancellation()
} finally {
disposeSomeStuff()
}
}
Sam
01/11/2023, 3:42 PMCLOVIS
01/11/2023, 3:42 PMSam
01/11/2023, 3:43 PMjw
01/11/2023, 3:48 PMJob
and add a completion listener
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/invoke-on-completion.html
it will be invoked synchronously if the job is already canceledjw
01/11/2023, 3:49 PMCLOVIS
01/11/2023, 3:49 PMbezrukov
01/11/2023, 6:45 PMbezrukov
01/11/2023, 6:46 PMfun CoroutineScope.invokeOnCancellation(block: () -> Unit) {
launch(start = CoroutineStart.ATOMIC) {
try {
awaitCancellation()
} finally {
block()
}
}
}