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

Francisco

06/15/2021, 4:35 AM
Hi 👋 I'm learning flows, and I have the following question. Is it possible for a flow to be collected by two different coroutines concurrently? I want the following properties • a collector shouldn't affect the other one • I want the flow to be able to use
Conflation
in case one of the collector is being to slow • flow is generated once (meaning I don't want to compute each item in the flow twice) I was able to do this using broadcast channel and consuming it as channel like
openSubscription().consumeAsFlow().conflate()
. Is is possible to do something similar to this using flows? Thanks!
also, if this is the wrong place to ask, sorry about that 😅
d

darkmoon_uk

06/15/2021, 8:00 AM
A
SharedFlow
with
replay = 1
fits those requirements.
To share any
Flow
look at the
.shareIn
operator
You might also consider
StateFlow
against your use case; this is similar to a
SharedFlow
but always holds a current, intrinsic value.
f

Francisco

06/15/2021, 4:05 PM
Thanks for the pointer! I will look at the docs
The docs about
SharedFlow vs BroadcastChannel
are great!