https://kotlinlang.org logo
#coroutines
Title
# coroutines
m

Marco Pierucci

03/19/2021, 9:56 PM
Hello everyone! Would :
Copy code
private val _event = MutableSharedFlow<S>(extraBufferCapacity = 1)
    val event: SharedFlow<S> get() = _event.asSharedFlow()
be a correct alternative to
Channels
for single shots events? Or would events would still be dropped if there are no subscribers
s

streetsofboston

03/19/2021, 10:41 PM
In my experience, without subscribers, calling
emit
will cause the event to be dropped.
This would make it wait for at least
n
subscribers:
Copy code
subscriptionCount.first { it >= n }
emit(value)
m

Marco Pierucci

03/20/2021, 2:17 AM
This suggest of using Channels, which I know is a valid alternative, my question was if the snippet I shared could work as a replacement for Channels
j

Javier

03/20/2021, 2:45 AM
In that post you can see a section about SharedFlow
m

Marco Pierucci

03/20/2021, 2:53 AM
Copy code
What about the use of SharedFlow? Can that help? Unfortunately, no. SharedFlow is hot. This means that during periods where the is no observer, say during a configuration change, events emitted on to the flow are simply dropped. Regrettably, this also makes SharedFlow inappropriate to emit events on.
You mean this?
👌 1
4 Views