I have like `2 flows` and I want to observe their ...
# coroutines
v
I have like
2 flows
and I want to observe their changes with
flatMapLatest
, if either of them emit new value, I want to trigger some action, but how can I observe on
2 flows
, should I combine them ? how can I do so
w
You can use
merge(flow1, flow2)
g
or combine(flow1, …) if you need result of both streams
v
when I was using
flatMapLatest
and inside lambda, I was receiving flow and
flatMapLatest
was returning that flow , but when I use
combine
, this will emit
Flow<Flow<>>
, so is there a way to achieve like flatMapLatest
g
Make sure that you have correct implementation of combine lambda
If you want to return Flow from lambda you then should add some additional operators like flattenMerge, depends on how you expect it should work
v
I did one thing, I used like this
Copy code
val someStateFlow = flow1.combine(flow2) { a, b ->
    recievingFlowFromRepository().first()
}.stateIn(...)
I used first() as I think I will not receive more values (usecase here is getting data from network) and then let the combine convert it to flow
o
This crosspost has been answered in #android