How can I map the values of one flow with another ...
# flow
t
How can I map the values of one flow with another that can (or not) be started later? I have flow
A
that will always be there and the flow
B
needs to be started with a id param to observe the database for the specific id, but there may be cases where it will never be init. How can I do this?
My solution was to create a flow
C
that start with an empty value and combines with
A
, then if I ever subscribe to
B
I collect and emit the values to
C
s
Could you not also not use a third flow but initialize your flow as you did and also add
.onStart { emit(...) }
? So you first emit something of your liking, and then it will continue emitting more once combine gets new values. Could that work?
t
I'm not sure I understand you well. The
onStart
would be just to emit a init value? If that's the case I'm just setting it when I start the flow at the moment
s
Yeah I thought your problem was that your other flow may never start (as you say “or not”) so you never even get an initial emit so it’s stuck waiting forever.
t
Maybe I just wasn't clear then 😅 I just mean that in some occasions I need to observe the flow
B
and some other occasions I do not need. So I implemented something similar to what you said, I've a third flow (
C
) and depending on the case I emit to it, otherwise it's just blank and it works fine 🙂
c
Perhaps
flatMapLatest
is what you need here? Sorry, your description is not totally clear; perhaps some code could help.