So I have a two mutable shared flows, Flow A and F...
# coroutines
j
So I have a two mutable shared flows, Flow A and Flow B. When Flow A triggers I want to use the latest value of Flow B, without having to react to when Flow B updates . I am also using the flow at other places so I don't want to cancel it. I tried combine but then I react to Flow B's updates. I then tried
zip
, but it cancels Flow B. I can get it to work with
replayCache()?.latestOrNull()
but that doesn't look good at all...
s
Then you should not combine them. Just let Flow B update a (state) variable/property when it emits a value. Use
map
on Flow A that uses it's emitted value and the variable/property mentioned above.
Or instead of the variable/property, use
flowB.value
inside
flowA.map { ... }
.
Copy code
flowA.map { calcValue(it, flowB.value) }
j
They are shared flows though... Not State Flows
But thanks. I think I managed to achieve something similar.
s
Ah, yes... shared, not state flows... Still you can create a state-flow out of the shared flow flowB and go from there... 🙂
👍 1
j
@streetsofboston yeah I kinda suggested the same thing when answering this SO question a few weeks back: https://stackoverflow.com/questions/68831421/kotlin-flow-how-to-combine-two-flows-and-only-emit-the-result-when-the-first-flo/68832423#68832423