https://kotlinlang.org logo
Title
c

Colton Idle

10/20/2021, 9:09 AM
If I want to create a flow, and then have some other method emit another item from that flow (think an onclick listener that emits a new item), would I just use StateFlow for that? Or is there some other flow I would use? I think stateFlow would work fine, but I'm more so curious if theres something else.
d

Dominaezzz

10/20/2021, 11:12 AM
Depends on how you want to handle conflation but that might work yeah.
You could also use a SharedFlow if you want more interesting behaviour.
k

K Merle

10/20/2021, 11:13 AM
Possibly you would wanna use SharedFlow as it will not emit initial value.
c

Colton Idle

10/20/2021, 11:30 AM
SharedFlow... hm. Okay I will look into it. thanks
c

Casey Brooks

10/20/2021, 1:50 PM
A
callbackFlow
might be a better choice, since it is cold. A
SharedFlow
is hot, so is always running, and is ideally meant for when you have multiple subscribers. An alternative would be to pass the items into a
Channel
and then do
channel.consumeAsFlow()
. This is useful when you want the items to get put into the channel anytime (not only when there are subscribers), but you only want each item handled once. It can also buffer the items until you actually start collecting from the channel, unlike
SharedFlow
which will drop anything emitted to it when there is no subscriber.