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

Tgo1014

01/25/2023, 12:00 PM
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

Stylianos Gakis

01/31/2023, 11:20 AM
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

Tgo1014

01/31/2023, 11:30 AM
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

Stylianos Gakis

01/31/2023, 11:36 AM
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

Tgo1014

01/31/2023, 12:16 PM
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

Chris Fillmore

02/21/2023, 2:50 AM
Perhaps
flatMapLatest
is what you need here? Sorry, your description is not totally clear; perhaps some code could help.
8 Views