Marko Mitic
05/08/2019, 12:28 PMaltavir
05/08/2019, 12:29 PMisActive
?Marko Mitic
05/08/2019, 12:30 PMfun CoroutineScope.launchSomething(){
launch {
ComplexClassWithState().run()
}
}
class ComplexClassWithState {
suspend fun run(){
while (isActive){
//do stuff
}
}
}
altavir
05/08/2019, 12:34 PMdelay(0)
, it will throw exception if the scope is canceled.Marko Mitic
05/08/2019, 12:35 PMfun CoroutineScope.launchSomething(){
launch {
ComplexClassWithState().run(this)
}
}
class ComplexClassWithState {
suspend fun run(scope: CoroutineScope){
while (scope.isActive){
//do stuff
}
}
}
delay(0)
does anything
public suspend fun delay(timeMillis: Long) {
if (timeMillis <= 0) return // don't delay
...
}
altavir
05/08/2019, 12:44 PMMarko Mitic
05/08/2019, 12:56 PMval isActive = runCatching {
suspendCoroutine<Boolean> { continuation ->
continuation.resumeWith(Result.success(continuation.context.isActive))
}
}.getOrDefault(false)
altavir
05/08/2019, 12:57 PMgergo
05/08/2019, 1:26 PMaltavir
05/08/2019, 1:28 PMgergo
05/08/2019, 1:32 PMaltavir
05/08/2019, 1:32 PMgergo
05/08/2019, 1:36 PMaltavir
05/08/2019, 1:38 PMgergo
05/08/2019, 1:40 PMaltavir
05/08/2019, 1:41 PMisActive
or suspension point.gergo
05/08/2019, 1:43 PMwhile(true) { yield(); /* do stuff */ }
, while coroutineScope{ while(isActive) { /* do stuff */ } }
also works, but it's not your recommendation, is that right?altavir
05/08/2019, 1:44 PMgergo
05/08/2019, 1:45 PMaltavir
05/08/2019, 1:46 PMcoroutineScope
+ isActive
won't throw exception at all (it could be better, no stack trace), but even if it would, it would do it for the whole scope.gildor
05/08/2019, 1:53 PMisActive isn't visible in suspend fun, it's part of CoroutineScopeisActive is extension property which isavailable in any suspend function, not only on scope, it just search for Job in coroutine context
altavir
05/08/2019, 1:55 PMgildor
05/08/2019, 1:56 PMaltavir
05/08/2019, 1:57 PMJob
and is marked as internal apigildor
05/08/2019, 1:58 PMcoroutineContext.isActive
altavir
05/08/2019, 2:01 PMsuspend fun doA(){
isActive
}
gildor
05/08/2019, 2:03 PMaltavir
05/08/2019, 2:04 PMMarko Mitic
05/08/2019, 2:05 PMgildor
05/08/2019, 2:20 PMkotlinx.coroutines.isActive
it was available before and even mentioned in docs now, but I cannot find it anymore in sources and even documentation link is broken