Hey there! What are my options if I want to retain...
# coroutines
s
Hey there! What are my options if I want to retain parent-child relationship in terms of making parent wait for children completion when
parent.join()
is invoked but prevent propagation of a cancellations from parent to children i.e. let children complete on it’s own without interruptions when
parent.cancel() / parent.cancelAndJoin()
is invoked? I create children using
async {}
blocks i.e.
Copy code
val parent = CoroutineScope(dispatcher).launch { 
...
    val childRes0 = async { ... }
    val childRes1 = async { ... }
...
}

...

parent.cancelAndJoin()
k
Wrap the children in
NonCancellable
?
s
Thanks Kevin, I’m reading what internet has on
NonCancellable
right now. I believe I have to adapt my code to something like
Copy code
val parent = CoroutineScope(dispatcher).launch { 
...
withConttext(NonCancellable) {
    val childRes0 = async { ... }
    val childRes1 = async { ... }
}
...
}

...

parent.cancelAndJoin()
what if my children are created in dedicated
coroutineScope
like
Copy code
fun runManyChildren() = coroutineScope {
    vals.map { x -> async { .... } }
}

val parent = CoroutineScope(dispatcher).launch { 
...
withConttext(NonCancellable) {
    runManyChildren()
}
...
}

...

parent.cancelAndJoin()
Will
runManyChildren
be uncancellable in this case?
k
yup
s
Got it, thanks for your help!
k
No problem!
u
be aware though, that even the canceled parent will only terminate after all children completed
s
Thanks Uli, that’s the desired behaviour in my case.
👍 1