am i missing a dependency. or can you not call lau...
# coroutines
c
am i missing a dependency. or can you not call launch from a suspend function 🤔
s
You can not... To switch a dispatcher use 'withContext' Or elaborate what are you trying to achieve
c
interesting. not sure how i didn't realize that till now. lol "solved" it for now by wrapper the launch with withContext like you said
Copy code
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
launch{...}
s
Or with
coroutineScope { .... launch { .... } .... }
The
coroutineScope
method brings the calling CoroutineScope (the scope in which the suspend function was called) into the lambda provided to it (its
this
receiver). Note that, due to structured concurrency, all launched (launch, async) children must finish/complete before the suspended call to
coroutineScope
resumes.
1
k
> The
coroutineScope
method brings the calling CoroutineScope (the scope in which the suspend function was called) into the lambda provided to it (its
this
receiver). > This is not true. The
coroutineScope
builder function creates a new coroutine scope that is a child of the coroutine executing the current suspending function. From the docs: > Creates a CoroutineScope and calls the specified suspend block with this scope. The provided scope inherits its coroutineContext from the outer scope, using the Job from that context as the parent for a new Job.
3
s
My suggestion was to only use withContext as from your example it looked like you only want to switch the dispatcher. In that scenario launch is not needed
☝️ 4
s
@kevin.cianfarini You're correct. The new CoroutineScope is a child of the calling one. The main point I was making is that all new coroutines launched from this one must finish/complete before this one completes.
👍 2
c
thanks all for teaching. learned something new. cheers
j
Copy code
withContext
suspend fun <T> withContext(
    context: CoroutineContext, 
    block: suspend CoroutineScope.() -> T
): T
Why is
block
an extension function of CoroutineScope? I thought
suspend CoroutineScope.()
Was discouraged as it's unclear if the block will launch things concurrently
Also I'm genuinely not sure. If call launch from within the block provided to
withContext
, will my code be suspended until the launched coroutine completes?
k
Yes,
withContext
also make a child coroutine scope that will wait for all launches coroutines to complete before it returns
s
block
is an extension suspend-lambda with a
CoroutineScope
as receiver much like the suspend-lambda you provide to a
launch
or
async
call. The coroutine framework guarantees that the suspend-lambda's CoroutineScope receiver (
this
) is the same scope in which the suspend-lambda is called, and that it's a CoroutineScope that will finish only when all its possible child coroutines have finished and the suspend-lambda resumes only after that point. If you create your own
suspend CoroutineScope.someFunction(...): SomeType
then that is not the case (unless you manually provide the mechanism/code that would do all that). That's why it's discouraged.