I’m trying to understand coroutines scopes. So at ...
# coroutines
j
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
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
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
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 🙂