https://kotlinlang.org logo
Title
b

bdawg.io

11/20/2018, 9:46 PM
For example
val rootJob = Job()
val rootContext = Dispatchers.Default + rootJob

val otherJob = Job()
val otherScope = object : CoroutineScope {
    override val coroutineContext = rootContext + otherJob
}
otherScope.doSomethingAsync()
otherScope.doSomethingElseAsync()

rootJob.cancel() // will this cancel items from otherJob?
d

Dico

11/20/2018, 9:51 PM
With this particular code, no. But normally, if you launch a coroutine in a scope with a Job, it is a child of that job. That relation isn't established when you do
rootContext + otherJob
, where the value associated with
[Job]
key is simply dropped.
b

bdawg.io

11/20/2018, 9:55 PM
So should I be checking if a
[Job]
is present and passing it as the
parent
of the
otherJob
? If I do that, will
otherJob
being cancelled cause the
rootJob
to cancel too (undesirably in my case)?
g

gildor

11/21/2018, 3:20 AM
What do you want to achieve? If you explicitly use different Jobs for different coroutines there are no relationships between them, in your example above you just replace one job with another.