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

jcechace

10/29/2018, 4:56 PM
I’m trying to understand coroutines scopes. So at top level I need a runBlocking function (e.g. main). Now if I have a another suspending function which uses async. Do I have to wrap the
async
calls into
coroutineScope {}
to inherit the coroutine scope from the calling function (e.g. main)?
l

louiscad

10/29/2018, 4:59 PM
If you don't need
async
(for parallelization inside one coroutine mainly), then don't use it, and prefer
withContext
and
launch
depending on the use case.
d

diesieben07

10/29/2018, 4:59 PM
As far as I understood Roman's talk at KotlinConf: Either have a non-suspending function that is an extension on
CoroutineScope
(that way any launched coroutines are attached to that scope). If you have a suspending function that also launches coroutines you need
coroutineScope
to inherit your own coroutine scope (the one that the suspending function is called with) to those coroutines.
If you just have a
suspend
function and not use
launch
, etc. then you do not need
coroutineScope
as far as I understand it.
j

jcechace

10/29/2018, 5:01 PM
sure, I’m aware that I need
coroutineScope
only if the function uses
async
or
launch
. Could you link the talk you are referring to please? Would watch!
got it
linked from docs 🙂
2 Views