Hi! Is there any blog post/other that explains why...
# flow
c
Hi! Is there any blog post/other that explains why flatMapMerge/flatMapConcat are not recommended? It says “most likely, suspending operations in map will be sufficient”, but that doesn't explain how to handle “I have a Flow, and I want to map each element to another flow”
w
why flatMapMerge/flatMapConcat are not recommended
Who says they’re not recommended? 🤔 Perhaps they meant that they’re often not exactly what you want: if you use
flatMapMerge
, then all of the flows you emitted are active at the same time, and their emissions are merged. That is if you have
fn(x) = flowOf(x1, x2, x3, …)
and do
flow.flatMapMerge { fn(it) }
then if
flow
emits
a, b, c, d, e
then after flat map you’l be getting
a1, b1, a2, c1, c2, b2, a3, b3
etc. Previously emitted flows don’t get cancelled
With
flatMapConcat
, the all the flows you emit wait until the previous one finishes.
So it’s just that their use cases are pretty rare and most of the time when converting an element to flow you’d want use
flatMapLatest
, because you’d not be interested in items emitted from flows created for previous value than the latest