james
05/14/2020, 7:34 PMsuspend fun doSomething() = withContext(coroutineContext) {
launch {
while(isActive) {
println("I'm Still Here")
delay(1000)
}
}
}
If that suspend function get’s cancelled due to an OOM exception how do I insure that the job that was launch is also cancelled?octylFractal
05/14/2020, 7:36 PMwithContext opens a new Job that is the child of whatever called the suspend function, and launch creates a new Job that is the child of withContext, it will all be cancelled automaticallyoctylFractal
05/14/2020, 7:41 PMjames
05/14/2020, 7:54 PMwithContext but that forces this to no longer be a background task in that this function does not resolve until the job resolve.
The naive approach would be something like this:
suspend fun doSomething() {
GlobalScope.launch {
while(isActive) {
println("I'm Still Here")
delay(1000)
}
}
}
Obviously cancellations don’t get respected correctly there, but i’m trying to figure out how to achieve the same background behavior with the ability to propogate the cancellations.octylFractal
05/14/2020, 7:55 PMisActiveoctylFractal
05/14/2020, 7:56 PMval job = coroutineContext[Job]!!
GS.launch {
while (isActive && job.isActive) { ... }
}james
05/14/2020, 8:16 PMoctylFractal
05/14/2020, 8:17 PMoctylFractal
05/14/2020, 8:17 PMjames
05/14/2020, 8:18 PM