Instead of LiveData, I'm using StateFlow in my Vie...
# flow
d
Instead of LiveData, I'm using StateFlow in my ViewModel to emit values to the UI. I realized that StateFlow doesn't emit equal values. How do you approach that? What if you want to emit multiple times the same value to show some message pop-up/toast/smth?
b
you can use SharedFlow, it doesn't conflate equal values
one more drawback of using StateFlow for your use case is that StateFlow is also drops emission if collector wasn't able to consume it before new one arrives. For example:
Copy code
state.value = Toast("Text A")
state.value = Toast("Text B")
state.value = Toast("Text C")
Collector [most likely, it depends on collector's Dispatcher] will receive only "Text C" It also can be fixed with shared flow by introducing extra buffer
d
Thanks!
j
if the state is not changing, what is the reason to redraw the scren?
d
As I mentioned, this might be a popup. For example a toast message that displays "success" every time an operation success
j
then you have to wonder if you are going to manage them as state or as event
d
You're right that it's more like an event. What would be the proper approach in that case? Is MutableSharedFlow still the way to go
j
and probably you should use channels instead of shared flow if you go for events
d
I see
Channels are used to handle events that must be processed exactly once* (see sidenote below for details).
another option is reset de state
but not sure if toast allow to know when they are disappearing
d
After reading the article, MutbaleSharedFlow still sounds as the best fit for the scenario
k
You can always wrap your event in some generic Event class. ( Or expand your base class) that will contain for example creation timestamp. So each event even with the same message will be different in terms of equality
d
This sounds more like a "fighting the framework" solution