https://kotlinlang.org logo
Title
k

kevin.cianfarini

03/23/2020, 8:15 PM
how do I cancel the children a coroutinescope and not the scope itself?
d

Dennis

03/23/2020, 8:19 PM
Put the children in another scope and cancel that
o

octylFractal

03/23/2020, 8:20 PM
or use
SupervisorJob()
k

kevin.cianfarini

03/23/2020, 8:22 PM
I'm not sure that would work, but I can try.
If parent job is specified, then this supervisor job becomes a child job of its parent and is cancelled when its parent fails or is cancelled. All this supervisor’s children are cancelled in this case, too. The invocation of cancel with exception (other than CancellationException) on this supervisor job also cancels parent.
I will report back
o

octylFractal

03/23/2020, 8:23 PM
to be clear,
SupervisorJob()
is for the scope you don't want cancelled
k

kevin.cianfarini

03/23/2020, 8:24 PM
yup, Ive implemented coroutine scope where it needs to. Testing now.
Seems to still cancel the whole scope
I've done it like this
CoroutineScope by CoroutineScope(Dispatchers.Main.immediate + SupervisorJob())
implementing CoroutineScope
o

octylFractal

03/23/2020, 8:25 PM
are you calling
cancel()
on the scope itself?
k

kevin.cianfarini

03/23/2020, 8:25 PM
When I need to cancel callback flows in this scope, I'm doing
this.cancel
which is the problem. It's cancelling the scope itself
And then I cannot recreate the flow later on when I need it because it cannot be collected in this scope
o

octylFractal

03/23/2020, 8:26 PM
it sounds like you should be creating another child scope that you cancel, rather than re-using this scope
k

kevin.cianfarini

03/23/2020, 8:27 PM
If I do that, then I would have to recreate the child scope every time I need to activate and deactivate the flow
(there are like 10 different flows bound to view events)
o

octylFractal

03/23/2020, 8:27 PM
if you must do it this way, you could always do
this.coroutineContext[Job]!!.children.forEach { it.cancel() }
1
k

kevin.cianfarini

03/23/2020, 8:27 PM
it seems to me that
CoroutineScope.cancelChildren
should be a thing
yeah, I'm going to do that
also might open an issue on Github
That worked. Thanks dude
d

Dominaezzz

03/23/2020, 8:44 PM
Pretty sure
cancelChildren
is indeed a thing.
k

kevin.cianfarini

03/23/2020, 8:45 PM
That is on job and not CoroutineScope
so I guess we could simplify the above code to:
this.coroutineContext[Job]!!.cancelChildren()
but, still. Wondering why it isn't on CoroutineScope itself
d

Dominaezzz

03/23/2020, 8:48 PM
this.coroutineContext.cancelChildren()
should also be possible iirc.
☝️ 1
k

kevin.cianfarini

03/23/2020, 8:49 PM
gotcha
Does that not still seem weird to you?
It seems non-intuitive to me.
d

Dominaezzz

03/23/2020, 8:50 PM
Not particularly.
CoroutineScope
!=
Job
. The separation of
CoroutineScope
,
CoroutineContext
and
Job
is a bit.... weird though, but I'm not sure if I could do better myself.
k

kevin.cianfarini

03/23/2020, 8:52 PM
The documentation of coroutinescope directly references the world
children
.
CoroutineScope should be implemented (or used as a field) on entities with a well-defined lifecycle that are responsible for launching children coroutines. Example of such entity on Android is Activity. Usage of this interface may look like this:
To me, that is justification enough itself to include a cancelChildren on this considering the other mechanisms that have children have this functionality.
a

araqnid

03/23/2020, 8:53 PM
tbh creating a new child job to put the flows into and then cancelling that sounds more right here to me
k

kevin.cianfarini

03/23/2020, 8:54 PM
Do you mean coroutineScope?
a

araqnid

03/23/2020, 8:55 PM
yes, coroutineScope is just creating a Job with the outer scope’s job as parent
k

kevin.cianfarini

03/23/2020, 8:57 PM
yes.
CoroutineScope.cancelChildren
would in effect be syntactic sugar for that. It feels cumbersome to be to necessitate making a singular ephemeral CoroutineScope for this kind of thing.
a

araqnid

03/23/2020, 9:00 PM
shrug it seems to be in line with how we have app/session/request “scopes” in other contexts — you’re running some flows to produce something, you put them in a Job so that all complete/get cancelled together
👍 1
g

gildor

03/25/2020, 3:28 PM
Not every scope has Job, GlobalScope doesn't. It's also true for context, but at least context is more local than Scope