Tianyu Zhu
05/01/2022, 10:07 PMmyFlow.collect { /* some work */ }
, is that work being done concurrently? If so, on how many threads? How do I control it?MR3Y
05/01/2022, 10:23 PMcollect()
is suspend fun
,so, it will suspend the caller thread until some work is done, if you want to concurrently collect some work you might be interested in launchIn(scope: CoroutineScope)
instead of collect()
Joffrey
05/01/2022, 10:25 PMcollect
is called sequentially on each element of the flow in the current coroutine. If you don't launch coroutines inside the collect
lambda, then the whole thing will be sequentialJoffrey
05/01/2022, 10:29 PMlaunchIn
will still not process the flow in multiple coroutines - only one will be launchedTianyu Zhu
05/01/2022, 10:41 PMcollet
lambda?Tianyu Zhu
05/01/2022, 11:24 PM<http://Dispatchers.IO|Dispatchers.IO> {
myFlow
.map { async { /* some work */ } }
.buffer()
.map { it.await() }
.collect { println(it) }
}
MR3Y
05/01/2022, 11:26 PMcollect {}
is being executed on another thread (or a pool of threads) concurrently and the the caller thread would continue to execute the next code block, but collect {}
will just suspend the current thread until the work is done that is why I suggested launchIn
that takes a scope parameter in which he can configure the scope to collect the flow in a different pool of threads and even control the number of threads in that pool. I hope you get my POV and feel free to correct me if I'm wrong.Joffrey
05/01/2022, 11:43 PM