Filip Stanis
02/07/2022, 4:42 PMFlow<List<Entity>>
I get from Room into either a SharedFlow
or a StateFlow
seems to prevent it from working (I get the initial list of items from Room, but it never updates). What could be causing this?
(code in first reply)Filip Stanis
02/07/2022, 4:45 PMval state = viewModel.state.collectAsState(State())
in every situation.
// This works as expected
val state = repository.getAll().map { State(items = it) }
// StateFlow - this doesn't
val state = repository.getAll().map { State(items = it) }.stateIn(viewModelScope, WhileSubscribed(5000), State())
// SharedFlow - same as StateFlow
val state = repository.getAll().map { State(items = it) }.shareIn(viewModelScope, WhileSubscribed(5000), 1)
(I'm aware I'm exposing a mutable flow here, that's just to keep the example simple)corneil
02/07/2022, 4:59 PMFilip Stanis
02/07/2022, 5:03 PMinterface MyDao {
@Query("SELECT * FROM Entity ORDER BY timestamp DESC")
fun getAll(): Flow<List<Entity>>
}
corneil
02/07/2022, 5:42 PMgetAll().collect
in order to let the flow emit values.Filip Stanis
02/07/2022, 5:51 PM@Singleton
. Why this caused an issue only for a StateFlow
/ stateIn
is a mystery.Chris Fillmore
02/08/2022, 3:24 PMFilip Stanis
02/08/2022, 3:36 PM