Hello everyone, I've a problem with my flow getting stuck in viewmodel init block. The flow is declared inside dataStoreRepository class, which actually successfully emit's desired value. But after collecting flow, it doesn't proceed inside the coroutineScope. Shared the code in thread:
here is to logcat after process death:
Can Korkmaz
02/22/2022, 2:31 PM
Here is the code:
Copy code
val suIdKeyFlow = dataStore.data.map { // this is dataStoreRepository class member
it[PreferenceKeys.suIdKey] ?: 0
}
ViewModel member function:
Copy code
private suspend fun readSuId(){
storeRepository.suIdKeyFlow.collect {
currentUserSuId.value = it
Log.d("main", "current user su id flow: ${currentUserSuId.value}") // log is printed to logcat
}
}
ViewModel init block:
Copy code
init {
Log.d("main","Main View Model created")// this gets printed to logcat
viewModelScope.launch(<http://Dispatchers.IO|Dispatchers.IO>){
Log.d("main","inside viewmodel coroutine scope 1")// this also gets printed to logcat
readSuId()
Log.d("main","inside viewmodel coroutine scope 2: --> ${currentUserSuId.value}}")// can't observe this log at logcat
if(currentUserSuId.value != 0){
getCurrentUser(currentUserSuId.value)
}
}
isUserLoggedIn()// this sets isLoggedIn mutableState<Boolean> in viewmodel, and observed at composable for automatic login, not necessarily related to problem
Log.d("main", " user logged in : ${userLoggedIn.value}")
t
Tim Oltjenbruns
02/22/2022, 6:04 PM
Collect blocks until the flow closes
c
Can Korkmaz
02/23/2022, 7:55 AM
@Tim Oltjenbruns Thanks. Seems like I need to practice flows again.