https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Marko Mitic

06/11/2019, 2:59 PM
How to cancel CoroutineScope and await/join all its jobs?
l

louiscad

06/11/2019, 3:14 PM
Launch them in a
coroutineScope { … }
. Simply.
Big thanks to the folks involved in structured concurrency.
m

Marko Mitic

06/11/2019, 3:16 PM
Any way to do it for an existing scope?
l

louiscad

06/11/2019, 3:21 PM
Yes, but why? Structured concurrency does it for free, you just have to delimit the local scope as I shown.
m

Marko Mitic

06/11/2019, 3:24 PM
I'm using structured concurrency to start several coroutines to handle my VPN queues
so those are indefinite until user stops the VPN and then I need to cancel those
I guess I'd need additional thread to utilize
coroutineScope { … }
for that and to refactor my scope
l

louiscad

06/11/2019, 4:21 PM
coroutineScope
itself doesn't need an extra thread.
m

Marko Mitic

06/11/2019, 4:49 PM
I've looked it up, yes it wouldn't need a thread
but I already have coroutines started in another scope
I fixed my issue in another way, so this isn't big issue for me
but I'm still interested whether there is some utility method to get scope's active jobs or to cancel existing scope and wait on it's jobs to finish
l

louiscad

06/11/2019, 9:31 PM
You can sort-of nest scopes with
coroutineScope
and they will wait for child coroutines and scopes
11 Views