ElawsDev
03/11/2024, 11:22 AMElawsDev
03/11/2024, 11:24 AMval userDataFlow : Flow<UserData> = appRepository.userDataFlow
I know I can convert this to a StateFlow and collect this StateFlow in the UI layer, but I just need to retrieve the values stored in datastore during initialization, perform some operations on them, and possibly update them later based on how user interacts with UI.
Is such scenario possible ? This is a basic usecase, and I'm surprised it's quite difficult to apprehend for a beginner.
I'm currently wondering if I shouldn't just manually write user data to a simple .json
file and fetch them during initialization.
Thank you very much for your help.ElawsDev
03/11/2024, 11:43 AMinit{
viewModelScope.launch{
userData = appRepository.userDataFlow.first()
// Then, initialize other variables in ViewModel who depends on the value of userData (and pass //these value to other functions to do the initialization).
}
}
Or :
val uiStateFlow = appRepository.userDataFlow
.map { userData -> calculateMyVariableFromUserData(userData) }
.stateIn(
viewModelScope,
SharingStarted.Eagerly,
defaultValue
)
init {
userData = uiStateFlow.value
}
Are these 2 approaches good ?Jrichards1408
03/11/2024, 3:59 PM