Vivek Sharma
02/09/2021, 1:58 PM2 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 itokarm
02/09/2021, 2:24 PMcombine
.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/combine.html
For 2 flows you can use either of the following forms:
Top level function
combine(f1, f2) { v1, v2 -> TODO() }
Flow.combine extension
f1.combine(f2) { v1, v2 -> TODO() }
Vivek Sharma
02/09/2021, 2:32 PMflatMapLatest
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 flatMapLatestVivek Sharma
02/09/2021, 2:32 PMval someStateFlow = flow1.combine(flow2) { a, b ->
recievingFlowFromRepository()
}.stateIn(...)
Vivek Sharma
02/09/2021, 2:32 PMFlow<Flow<>>
because combine returns by making a flow inside a lambdaokarm
02/09/2021, 2:38 PMparamFlow1.combine(paramFlow2) { a, b ->
Pair(a, b)
}.flatMapLatest { (a, b) -> // destructured Pair
loadWithParameters(a, b) // this returns a Flow
}.stateIn(...)
Vivek Sharma
02/09/2021, 2:57 PM