Jorge DomÃnguez
05/06/2022, 5:08 PMprivate val viewModelState = MutableStateFlow(HomeViewModelState(isLoading = true))
val uiState = viewModelState
.map { it.toUiState() }
.stateIn(
viewModelScope,
SharingStarted.Eagerly,
viewModelState.value.toUiState()
)
My question is, what's the advantage of using stateIn()
here? I would normally do something like:
private val _viewModelState = MutableStateFlow(HomeViewModelState(isLoading = true))
val viewModelState get() = _viewModelState
But looking into the stateIn()
docs there's a mention to increased performance for multiple observers since the upstream flow is instantiataed only once, but what if the flow is collected from a single composable? is there really an advantage there?
I was thinking that each recomposition can be considered as a new observer, in which case I can see how the use of stateIn()
helps, but I'd like to fully understand the implications of its usage and how it's better, so if anyone can shed some light I'd be grateful.Jorge DomÃnguez
05/06/2022, 5:11 PMAdam Powell
05/06/2022, 6:15 PMUiState
, it's something that gets mapped to one, and if you don't have a StateFlow
then you don't have an initial value available, you have to subscribe and wait for it to emit, and in the case of consuming from compose that means you get a frame of empty data before the first item is known.Adam Powell
05/06/2022, 6:16 PMprivate var viewModelState by mutableStateOf(HomeViewModelState(isLoading = true))
val uiState: UiState
get() = viewModelState.toUiState()
to leverage snapshots instead and skip all of the subscription complexity, since the upstream source of truth is a MutableStateFlow
anyway - a hot data source that doesn't care about subscription awareness in the first place.Jorge DomÃnguez
05/06/2022, 6:36 PMcollectAsState()
?Jorge DomÃnguez
05/06/2022, 6:38 PMAdam Powell
05/06/2022, 7:43 PM.collectAsState
is an observer for as long as it remains in composition with the same receiver flow instanceAdam Powell
05/06/2022, 7:43 PMFlow.collect
persists undisturbed across recompositions for as long as the call to .collectAsState
is present in the composition for the same Flow
instanceJorge DomÃnguez
05/06/2022, 7:51 PM