https://kotlinlang.org logo
#flow
Title
# flow
s

Smorg

11/17/2020, 7:34 PM
Hi, beginner flow user here, please what operator do I need to conditionally run a flow
flow2
based on the condition of an upstream flow
flow1
? this is what I have tried but I am not sure if I actually should be doing the tagged line
Copy code
flow1()
 .flatMapLatest { res ->
   if(res.conditionIsFalse) {
     flowOf() // works but wondering if there is an operator that doesn't require this
   } else {
     flow2()
   }
 }
 .onEach { flow2Res -> 
   // do something with flow2Res
 }
 .launchIn(scope)
w

wasyl

11/17/2020, 7:43 PM
flatMapLatest
is pretty much what you want, you can use
emptyFlow()
to not emit anything.
s

Smorg

11/17/2020, 7:44 PM
thanks @wasyl, looks like what I am doing works then.
w

wasyl

11/17/2020, 7:44 PM
Be aware of the difference between
flatMapMerge
,
flatMapConcat
and `flatMapLatest`: the first one will run multiple inner flows simultaneously and emit their values as they come,
concat
version will also run inner flows but maintain order in which they were triggered (so concat their emissions), and
latest
one will cancel the previous inner flow once the new item is handled
👍 2
s

Smorg

11/17/2020, 7:48 PM
makes sense @wasyl, thanks a lot. I am just understanding what the
flatMapMerge
doc means for the first time with your explanation 😅
👍 1
3 Views