https://kotlinlang.org logo
Title
o

Ovsyannikov Alexey

10/06/2021, 12:06 PM
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

y9san9

10/06/2021, 12:24 PM
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

Ovsyannikov Alexey

10/06/2021, 12:35 PM
I will try, but I am not pretty sure that it will work
Thanks :)
y

y9san9

10/06/2021, 12:37 PM
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

Ovsyannikov Alexey

10/06/2021, 12:42 PM
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

y9san9

10/06/2021, 1:00 PM
This is the idea of the replay 🙂 It should replay data when new subscriber comes
o

Ovsyannikov Alexey

10/06/2021, 1:01 PM
i do not need replay, I need to send accumulated data once to all available subscribers and then forget this data
y

y9san9

10/06/2021, 1:01 PM
For second point, is it solution to
suspend
, but not accumulate?
o

Ovsyannikov Alexey

10/06/2021, 1:01 PM
as I understand, replay will keep data until that data will not be out of range of
replay
count parameter
y

y9san9

10/06/2021, 1:02 PM
and replay will keep this data and replay it for any new subscriber 😁
o

Ovsyannikov Alexey

10/06/2021, 1:02 PM
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

Adam Powell

10/07/2021, 12:56 PM
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