https://kotlinlang.org logo
Title
p

pablisco

01/24/2022, 2:59 PM
I was wondering. Is this:
flow.flatMapLatest {
  flow {
    emit("a")
    emit("b")
    emitAll(otherFlow())
  }
}
the same as this?
flow.transformLatest {
  emit("a")
  emit("b")
  emitAll(otherFlow())
}
They seem to do the same on paper (minus the extra flow object), however, is there anything that using
flatMapLatest
will do that
transformLatest
doesn’t? 🙂
j

Joffrey

01/24/2022, 3:04 PM
flatMapLatest
literally calls
transformLatest
under the hood, and simply emits all items from the flow returned by the lambda. So I don't believe there is any real difference. I'd say it's mostly a matter of expressing better what the code does, and avoiding the extra flow
Also, it might be useful to use
flatMapLatest
if you need to do some extra stuff before creating the new flow for a given element of the source flow
p

pablisco

01/24/2022, 3:17 PM
Oh yeah, you are right, it does. Should have checked the source 😅