spand
10/11/2018, 6:13 AMclass Activity : CoroutineScope {
var job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Default + job
init {
val workJob = launch(start = CoroutineStart.LAZY) {
// Doing stuff
delay(5000)
}
launch(start = CoroutineStart.ATOMIC) {
try {
workJob.join()
} finally {
println("Cleaning up")
}
}
}
fun destroy() {
job.cancel()
}
}
gildor
10/11/2018, 6:15 AMclass Activity : CoroutineScope {
override val coroutineContext = Dispatchers.UI + Job()
override fun onDestroy() {
coroutineContext.cancel()
}
}
gildor
10/11/2018, 6:17 AMinit
block should show, doesn’t make a lot of sense for me with all those ATOMIC and additional lazy launch
But maybe I don’t see some important usecasespand
10/11/2018, 6:18 AMspand
10/11/2018, 6:19 AMjob.join()
will return after cleanupgildor
10/11/2018, 6:23 AMinvokeOnCompletion
?spand
10/11/2018, 6:24 AMgildor
10/11/2018, 6:24 AMspand
10/11/2018, 6:24 AMgildor
10/11/2018, 6:24 AMgildor
10/11/2018, 6:24 AMgildor
10/11/2018, 6:29 AMspand
10/11/2018, 6:38 AMgildor
10/11/2018, 6:38 AMgildor
10/11/2018, 6:39 AMspand
10/11/2018, 6:39 AMgildor
10/11/2018, 6:39 AMgildor
10/11/2018, 6:40 AMgildor
10/11/2018, 6:40 AMgildor
10/11/2018, 6:40 AMgildor
10/11/2018, 6:41 AMspand
10/11/2018, 6:48 AMlaunch
will just be cancelled. Setting the NonCancellable context gives the appropriate behaviour.gildor
10/11/2018, 6:49 AMspand
10/11/2018, 6:49 AMspand
10/11/2018, 6:53 AMgildor
10/11/2018, 7:05 AMlaunch {
try {
// Doing stuff
delay(5000)
} finally {
println("Cleaning up")
}
}
gildor
10/11/2018, 7:06 AMDico
10/11/2018, 1:29 PMDico
10/11/2018, 1:29 PMkingsley
10/11/2018, 1:49 PMwithContext(NonCancellable) { ... }
in finally I believe
But yea, Andrey already hit on all the points to consider