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

Vivek Sharma

02/09/2021, 10:05 AM
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

wasyl

02/09/2021, 10:10 AM
You can use
merge(flow1, flow2)
g

gildor

02/09/2021, 10:15 AM
or combine(flow1, …) if you need result of both streams
v

Vivek Sharma

02/09/2021, 10:32 AM
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

gildor

02/09/2021, 11:08 AM
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

Vivek Sharma

02/09/2021, 11:16 AM
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

okarm

02/09/2021, 2:48 PM
This crosspost has been answered in #android
3 Views