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

myanmarking

01/02/2020, 4:42 PM
Copy code
storage.findXXX(userId)
    .flatMapLatest {
        if(it == null){
            flowOf(
                storage.findYYY(userId)
            )
        } else{
            flowOf(it)
        }
    }
z

Zach Klippenstein (he/him) [MOD]

01/02/2020, 4:44 PM
In this example, I don't understand why you're wrapping either return value in
flowOf
, since there's only one value in either case. Presumably your actual code has multiple values?
m

myanmarking

01/02/2020, 4:45 PM
yes. both have multiple values. i want to use alternative flow while the original emits null
z

Zach Klippenstein (he/him) [MOD]

01/02/2020, 4:47 PM
Another alternative would be to use the
transform
operator:
Copy code
.transform { 
  if (it == null) {
    emit(…)
    emit(…)
    // or
    emitAll(otherFlow)
  } else {
    emit(it)
  }
See https://kotlinlang.org/docs/reference/coroutines/flow.html#transform-operator
m

myanmarking

01/02/2020, 4:47 PM
hm, i think thats wat i am after. thanks!
p

Paul Woitaschek

01/02/2020, 6:03 PM
It should be transformLatest, else you'll accumulate streams