In the proccess of migration I found out some things that I would like to comment out with people who have good knowledge about this. To be honest, I am not master of flow, used livedata for long time, so now I would like to figure out as much as I can about flow from the start.
While working with it I found out that most of the time instead of liveData, I would use StateFlow (much much more than SharedFlow). But what Are the cases of using SharedFlow I asked myself. I came across this one totally by accident.
I had livedata in situation where I emitted boolean value when user long clicks on the row of recyclerView. click would post true value and I would react in my fragment. Now I tried to change it in stateflow:
val dataInserted: MutableStateFlow<Boolean> = MutableStateFlow(false)
// more code
dataInserted.emit(true)
And in my fragment, I collect data:
CoroutineScope(Dispatchers.Main).launch {
mainViewModel.dataInserted.collect {
// doSomething()
}
}
but I found out that my doSomething() function is called only one time. With livedata this worked well, but with stateFlow, I guess it needs different value to react. If you send true 2 times, or 3 times, it will react only one time since it gets the same value. Is my assumption true on this one?
Anyways after this I decided to implement Sharedflow here and it works well. Why does this work with sharedFlow and not with stateFlow. Does SharedFlow drop value after it gets collected, so after it collets same value again, it can react again. this is my second assumption and I would like someone to confirm or deny me and explain if I am wrong.
Also are state and shared flow enough to completely replace whole livedata. Is there anything else or these 2 guys can solve all my needs. Thanks in advance