reactormonk
04/23/2024, 4:29 PMsuspend fun someFun(flow: Flow<A>): Unit {
withContext(Dispatchers.Default) {
launch {
flow.collect { }
}
}
}
return immediately?
So I did
suspend fun someFun(flow: Flow<A>): Unit {
println("0")
withContext(Dispatchers.Default) {
println("1")
launch {
println("2")
flow.collect { }
}
println("3")
}
println("4")
}
and got
0
1
3
2
WTF?reactormonk
04/23/2024, 4:39 PMCoroutineScope(coroutineContext).launch {
Sebastian Lehrbaum
04/23/2024, 4:47 PMreactormonk
04/23/2024, 4:48 PM3
in the outputSebastian Lehrbaum
04/23/2024, 4:58 PMjw
04/23/2024, 4:58 PMreactormonk
04/23/2024, 4:59 PMjw
04/23/2024, 5:00 PMuli
04/25/2024, 11:11 AMwithContext
docs say nothing about waiting for a scope to complete:
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result
I thought for such a behaviour you’d need a coroutineScope { }
blockSebastian Lehrbaum
04/25/2024, 11:16 AMuli
04/25/2024, 11:23 AMSebastian Lehrbaum
04/25/2024, 11:26 AMSebastian Lehrbaum
04/25/2024, 11:28 AMuli
04/25/2024, 11:28 AMDispatchers.Default
? Would it also wait for the launched coroutine? I.e. is the result of this the same? (Note the with context on top of the method)
suspend fun someFun() = withContext(Dispatchers.Default) {
println("0")
withContext(Dispatchers.Default) {
println("1")
launch {
println("2")
delay(10000)
}
println("3")
}
println("4")
}
Sebastian Lehrbaum
04/25/2024, 11:28 AMSebastian Lehrbaum
04/25/2024, 11:29 AMjw
04/25/2024, 11:29 AMSebastian Lehrbaum
04/25/2024, 11:29 AMuli
04/25/2024, 11:29 AMjw
04/25/2024, 11:30 AMSebastian Lehrbaum
04/25/2024, 11:30 AMuli
04/25/2024, 11:34 AMblock
) is an extension on CoroutineScope
suspend fun <T> withContext(context: CoroutineContext, block: suspend CoroutineScope.() -> T): T
Sebastian Lehrbaum
04/25/2024, 11:35 AM