dimsuz
01/13/2022, 9:25 AMCoroutineScope
and then I have dynamically created another scope, how could I add this scope to the first scope, so that second is cancelled along with the first?Joffrey
01/13/2022, 9:30 AMJob
part of the scopes. If you want to establish a parent-child relationship between scopes, you can use their jobs:
val someParentScope: CoroutineScope = TODO("get the parent scope from somewhere")
val yourChildScope = CoroutineScope(Job(parent = someParentScope.coroutineContext.job))
Joffrey
01/13/2022, 9:32 AMdimsuz
01/13/2022, 9:34 AMJoffrey
01/13/2022, 11:39 AMsomeParentScope.coroutineContext.job.invokeOnCompletion { ... }
to register a callback that cancels the child jobdimsuz
01/13/2022, 11:44 AMclass Feature {
private var featureScope: CorotineScope
fun start(parentScope: CoroutineScope) {
featureScope = parentScope + CoroutineScope()
}
}
// elsewhere in Router
someFeature.start(routerScope)
// and then
router.cancel() // cancels feature too
Thank you!Joffrey
01/13/2022, 11:47 AMdimsuz
01/13/2022, 11:48 AMJoffrey
01/13/2022, 11:49 AMNick Allen
01/15/2022, 12:34 AMfeatureScope = parentScope + CoroutineScope()
CoroutineScope()
creates a Job
which is not connected to parentScope
at all and that will overwrite the Job
in parentScope
when creating featureScope
.dimsuz
01/16/2022, 5:56 PMfeatureScope = CoroutineScope(SupervisorJob(parentScope?.coroutineContext?.job) + handler + dispatcher)
which ties supervisor job to the parent scope's job if provided.