Hello, I’m trying to use `StateFlow` to handle a k...
# coroutines
w
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
SharedFlow is what you need
w
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
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
Oh I see, thanks!