Ruben Quadros
03/31/2022, 1:20 PMList<Flow<T>>
what is the best way to keep collecting from each Flow<T>
?Joffrey
03/31/2022, 1:20 PMRuben Quadros
03/31/2022, 1:25 PMcoroutineScope.launch {
list.forEach {
eachFlow.collect {
//do something
}
}
}
Joffrey
03/31/2022, 1:26 PMRuben Quadros
03/31/2022, 1:28 PMJoffrey
03/31/2022, 1:29 PMRuben Quadros
03/31/2022, 1:30 PMJavier
03/31/2022, 1:32 PMlist.forEach { eachFlow ->
coroutineScope.launch {
eachFlow.collect {
//do something
}
}
}
archecraft
03/31/2022, 1:34 PMlist.forEach { flow ->
flow.onEach {
// do something
}.launchIn(coroutineScope)
}
?Joffrey
03/31/2022, 1:34 PMmerge()
here. It doesn't combine elements of different flows into one, it just collects them all in a single flow:
list.merge().collect {
// do stuff on each element, which could come from any flow
}
This abstracts away the multiple coroutines, and doesn't even require a scope, unless you want to launch this multi-collection concurrently as wellRuben Quadros
03/31/2022, 1:38 PMRuben Quadros
03/31/2022, 1:48 PMJoffrey
03/31/2022, 1:50 PMmerge()
doesn't wait for anything, it returns immediately a Flow<T>
. If you then collect
this flow directly, collect
will suspend until all flows are done.
If you want that to be asynchronous, you can of course run it in a coroutine:
coroutineScope.launch {
list.merge().collect {
// do stuff
}
}
But yes the other 2 solutions above work fine tooJoffrey
03/31/2022, 1:54 PM