https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

spierce7

12/02/2020, 4:13 AM
If I have a
List<Flow<String>>
and I want to merge them all into a single
Flow<String>
, what’s the best way to do that?
c

chris-horner

12/02/2020, 4:40 AM
Copy code
flows.merge()
z

Zach Klippenstein (he/him) [MOD]

12/02/2020, 4:52 AM
How do you want to merge them? Interleaved, concatenated, something else? There’s a merge function that takes flows. You can also turn your list into a Flow, then use
flattenConcat
or
flattenMerge
.
s

spierce7

12/02/2020, 4:55 AM
I just want whenever one of the children flows emits an event, I want it to turn into an event in the parent flow
i.e. I’m not wanting to do something like an rx zip operator, which I’m assuming is what you’d call
interleaved
?
z

Zach Klippenstein (he/him) [MOD]

12/02/2020, 5:22 PM
What I meant by “interleaved” is what you want. Vs “concatenated” which means you emit all the items of one flow until it completes, and only then start emitting items from the next flow.
You can use one of the merge operators for your case.
3 Views