https://kotlinlang.org logo
Title
s

Simon Lin

11/16/2020, 5:42 AM
What is the alternative using flow or channel for SingleLiveEvent in this situation:
val onSuccess = SingleLiveEvent<Unit>()

fun load() {
    // do something
    onSuccess.call()
}
a

Ali Albaali

11/16/2020, 6:28 AM
You can use SharedFlow https://github.com/Kotlin/kotlinx.coroutines/issues/2034
:thumbsup_all: 2
t

tseisel

11/16/2020, 8:04 AM
Some points about using SharedFlow for dispatching single events: - Events are lost when there are no subscribers - Multiple subscribers will all receive the same event
a

Andrea Nicoletti

11/16/2020, 8:26 AM
Like the others said, you can use a SharedFlow with replay = 0 so that the new value will be emitted only once and not repeated
m

Michael Ferguson

11/17/2020, 2:19 PM
I don’t like using SharedFlow on Android to handle “events”. I like to use a channel with
receiveAsFlow
so that on configuration change if an event is emitted with no observers it’s not dropped. (The channel buffers while there are no observers while SharedFlow is hot.) There are some caveats when doing that though. https://medium.com/@elizarov/shared-flows-broadcast-channels-899b675e805c Has a really good explanation of that “single shot event” use case.
☝️ 1