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

wbertan

11/24/2020, 3:34 PM
Hello, I’m trying to use
StateFlow
to handle a kind of
Event
, but facing issues when trying to set the same value again (for example when the user clicks the same button again, triggering same event). Found this GitHub issue: https://github.com/Kotlin/kotlinx.coroutines/issues/2011 With @elizarov comment:
Copy code
We'll provide a different, more flexible primitive, for those rare cases when you don't want equality-based conflation. Stay tuned.
Do we have anything in the latest release to handle this scenario?
b

bezrukov

11/24/2020, 3:41 PM
SharedFlow is what you need
w

wbertan

11/24/2020, 3:47 PM
Amazing! Thanks @bezrukov Do you predict any issue in setting up with:
Copy code
MutableSharedFlow<Event>(1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
Basically I want to use the
tryEmit
in non
suspend
functions.
b

bezrukov

11/24/2020, 3:50 PM
if you will send more than you can process some events will be lost, so I suggest adding some buffer:
Copy code
MutableSharedFlow<Event>(1, extraBufferCapacity = ... , onBufferOverflow = BufferOverflow.DROP_OLDEST)
you're also can set replay = 0 (first param), because most likely you are not interested about event that happened before subscription
w

wbertan

11/24/2020, 3:52 PM
Oh I see, thanks!
3 Views