How to cancel CoroutineScope and await/join all it...
# coroutines
m
How to cancel CoroutineScope and await/join all its jobs?
l
Launch them in a
coroutineScope { … }
. Simply.
Big thanks to the folks involved in structured concurrency.
m
Any way to do it for an existing scope?
l
Yes, but why? Structured concurrency does it for free, you just have to delimit the local scope as I shown.
m
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
coroutineScope
itself doesn't need an extra thread.
m
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
You can sort-of nest scopes with
coroutineScope
and they will wait for child coroutines and scopes