gts13
06/05/2024, 2:40 PMMichael Krussel
06/05/2024, 2:45 PMView
object, that must happen on the main thread. Pretty much all interaction with Views
must be on the main thread.
If you are talking about changing some state property like with MVVM, then it depends on the thread safety of that and how that eventually gets to the UI.
For Compose, you don't directly interact with the UI but instead with just state, so it depends on what you are using for storing the state.gts13
06/05/2024, 3:10 PMflowOn
the collection (in the viewmodel) with a default dispatcher then what are the implications there?Michael Krussel
06/05/2024, 3:11 PMgts13
06/05/2024, 3:12 PMsealed interface ExampleUiState : UiState {
data object Loading : ExampleUiState
data class Data(
val accounts: Set<Account> // Account is a data class with three string properties
) : ExampleUiState
}
gts13
06/05/2024, 3:16 PMMichael Krussel
06/05/2024, 3:16 PMgts13
06/05/2024, 3:18 PMinit {
myRepository.getAccounts()
.onEach { accounts ->
_state.emit(
ExampleUiState.Data(
accounts = accounts
)
)
}
.flowOn(dispatcher) //<- here to use the default dispatcher
.launchIn(viewModelScope)
}
Michael Krussel
06/05/2024, 3:19 PMgts13
06/05/2024, 3:20 PMprotected val _state: MutableStateFlow<T> by lazy { MutableStateFlow(initialState()) }
val state: StateFlow<T>
get() = _state.asStateFlow()
Michael Krussel
06/05/2024, 3:27 PMgts13
06/05/2024, 3:28 PMRobert Williams
06/05/2024, 3:44 PMflowOn
above onEach
(or even better inside your implementation of getAccounts
because it shouldn't be caller's responsibility to know about this)?