matej
06/23/2020, 12:58 PMJob
(Kotlin native) to finish whatever it was doing after calling cancel()
?
Assume the simple example of a `launch`ed Job
that does something in a while(isActive)
loop. When it exits the loop it would need to run a few more lines of cleanup before it completes. As it stands, after calling cancel()
my job is getting terminated outright, and the lines after the while loop never get executed. I'm left scratching my head.streetsofboston
06/23/2020, 1:22 PMtry {
while (isActive) {
...
}
} finally {
// a few more lines here, clean up, etc
...
}
matej
06/23/2020, 1:25 PMManuel Wrage
06/23/2020, 1:28 PMstreetsofboston
06/23/2020, 1:31 PMtry {
while (isActive) {
...
}
} finally {
cleanUpScope.launch { /* a few more lines here, clean up, etc */ }
}
matej
06/23/2020, 2:12 PMmatej
06/23/2020, 2:44 PMtry {
while (isActive) {
...
}
} finally {
withContext(NonCancellable) { /* a few more lines here, clean up, etc */ }
}
With the added benefit that calling join
on the main job also seems to work as expected, and waits for the NonCancellable bit to complete.