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

spierce7

12/02/2020, 4:24 AM
If I have access to a
CoroutineContext
via
coroutineContext
, what is the best way for me to create child coroutines? I’m assuming I need to create a
CoroutineScope
, but there aren’t any utility methods I’ve found for this, making me think that there might be a better way?
z

Zach Klippenstein (he/him) [MOD]

12/02/2020, 4:48 AM
To use structured concurrency, use
coroutineScope {}
. Otherwise, you can use
CoroutineScope(coroutineContext)
s

spierce7

12/02/2020, 4:57 AM
I looked at the source for
coroutineScope {}
. It doesn’t look like it uses the current scope. If I call
coroutineScope {}
inside a coroutine, will it create child coroutines?
b

bezrukov

12/02/2020, 6:31 AM
Yep it takes your current context and creates child scope. coroutineScope {} suspends until all children completes
z

Zach Klippenstein (he/him) [MOD]

12/02/2020, 5:21 PM
Yes it does, that’s pretty much its main use case.
3 Views