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

natario1

03/20/2021, 6:37 PM
What's the elegant way to have something like
combine(flow1, flow2, ..., flowN, transform)
that does not wait for all flows to emit the first value? I'd happily receive a smaller array if some flow does not have a value yet.
e

elizarov

03/20/2021, 8:54 PM
You can do
onStart { emit(null) }
on each on the source flows to have them immediately emit a null. What are you trying to achieve, though?
n

natario1

03/20/2021, 9:08 PM
Yeah I might be doing things the wrong way? I have a
List<Flow<T>>
where each flow retrieves a single
T
item. Retrieval is very expensive. Then I want a
Flow<List<T>>
including all items that satisfy a certain condition, so I'm using
combine(*flows) { it.filter(condition) }
. Works but, waits for all flows, while I'd like to receive incomplete results as well.
Is
onStart { emit(null) }
equivalent to
flow { emit(null); emitAll(flow) }
?
👌 1
e

elizarov

03/21/2021, 7:21 AM
It might be easier just to do
list.asFlow().flattenMerge(list.size).filter { … }
n

natario1

03/21/2021, 11:29 AM
Thanks. I think that works great if the source flows are guaranteed to emit just once, but what I have is state flows which might emit updated values of T over time
2 Views