samuel
02/19/2021, 11:09 PMreplaceState
works as expected with the subscriber getting the new value, however, calling updateState
doesn’t. Does any one have a clue why this happens?samuel
02/19/2021, 11:11 PMsealed class State {
object FirstState: State()
object SecondState: State()
}
class StateHolder {
private val _state: MutableStateFlow<State> =
MutableStateFlow(LinkedList(listOf(State.FirstState)))
val state: StateFlow<State> =
_state.mapLatest{ it.peekFirst() }
.stateIn(GlobalScope, SharingStarted.Eagerly, State.FirstState)
// works
fun replaceState(newState: State) {
_state.value = LinkedList(listOf(newState))
}
// Doesn't work
fun updateState(newState: State) {
_state.value = _state.value.apply { push(newState) }
}
}
// Called from another class
myScope.launch {
state.collectLatest { state ->
Timber.e("state value $state")
}
}
sealed class State {
object FirstState: State()
object SecondState: State()
}
Dominaezzz
02/19/2021, 11:32 PMStateFlow
doesn't update subscribers if the value is set to the same object.Dominaezzz
02/19/2021, 11:32 PMsamuel
02/19/2021, 11:38 PMMichael Ferguson
02/20/2021, 12:04 AMStateFlow
uses .equals
to compare and conflate values. So attempting to force an emission by simply copying/clone an object into a new object will fail.Michael Ferguson
02/20/2021, 12:05 AMLiveData
in that respect.samuel
02/20/2021, 12:13 AM