Hello everybody :) I wished to ask about flows beh...
# coroutines
o
Hello everybody :) I wished to ask about flows behaviour, especially in context of
MutableSharedFlow
- what shared flow will do in case if nobody is listening for its stream? As I understand, currently all hot flows will throw out data when there are nobody to get it. Or I missed something? If not - is there some built-in flow which allow to prohibit trashing of data in case when nobody listetning that flow?
y
You can specify the type of SharedFlow and make it lazy, so it will be suspended at first emit until someone will subscribed ๐Ÿ™‚
probably you need to use
shareIn
and
SharingStarted.Lazily
or buffer with onBufferOverflow = Suspend
o
I will try, but I am not pretty sure that it will work
Thanks :)
y
I will make a little example in 20 minutes (I'm not at home now) ๐Ÿ‘Œ
๐Ÿ™ 1
๐Ÿ‘Œ 1
Okay, onBufferOverflow will have no effect without subscribers. Can you please decribe your case, so I can tell you if shareIn is usable here.
o
I think, I should use
WhileSubscribed
instead of
SharingStarted.Lazily
in my case
โž• 1
Trying ๐Ÿ™‚
It looks like almost what I need, but there is a small problem: replay of
shareIn
will replay data in each new subscriber. I need next behaviour: โ€ข When there is at least one subscriber - emit to all subscribers currently accumulated data โ€ข When there are no any subscriber - accumulate data โ€ข Any sent data from flow must be removed and not repeated in any new subscribers
y
This is the idea of the replay ๐Ÿ™‚ It should replay data when new subscriber comes
o
i do not need replay, I need to send accumulated data once to all available subscribers and then forget this data
y
For second point, is it solution to
suspend
, but not accumulate?
o
as I understand, replay will keep data until that data will not be out of range of
replay
count parameter
y
and replay will keep this data and replay it for any new subscriber ๐Ÿ˜
o
I do not need it when there is at least one subscriber ๐Ÿ™‚
what I need is to accumulate data only when there are no any subscribers and clean any sent data
a
What is the difference between losing an event because no subscribers were listening to a shared flow and "losing" an event because the right subscriber wasn't listening yet, but some other subscriber was? You're referring to sending accumulated data to "all available" subscribers when going from zero subscribers to not-zero and releasing the accumulated data, but that transition point will always be when that count goes from 0-1, not 0-2 or 0-30, and all subscribers after the first won't see the accumulated data. Flows have no API for starting more than one subscription atomically.
๐Ÿ‘ 1