which is best way when creating a self contained f...
# coroutines
v
which is best way when creating a self contained feature? and why?
Copy code
class Feature(parentScope: CoroutineScope) {
   ...
}
or
Copy code
class Feature(parentContext: CoroutineContext): CoroutineScope { 
    override val coroutineContext = parentContext + SupervisorJob() 
    ...
}
1️⃣ 4
d
(1) is more transparent about the lifetime of the coroutines created in
Feature
.
parentScope
controls that. In (2), you have to look at the source code to determine how the
Job
passed in
parentContext
is used, and the answer, surprisingly, is that it's not used at all. There's a third option: create a
SupervisorJob
whose parent job is taken from
parentContext
, which will allow cancelling the coroutines from the outside at least, but that is still less self-evident than (1).
v
Thanks @Dmitry Khalanskiy [JB]
🙂 1