Ellen Spertus
04/13/2020, 10:46 PMRoomDatabase
. I can’t access the database (or even the repository) directly from the fragment’s onStart()
method, since database access is not allowed on the UI thread. What’s the right way for onStart()
to initiate a database query on the IO
thread and then a callback on the UI thread?override fun onStart() {
super.onStart()
viewModel = buildModel()
viewModel.viewModelScope.launch {
checkForNickname()
}
}
private suspend fun checkForNickname() {
val entity = viewModel.getContact() // suspend fun
withContext(Dispatchers.Main) {
// do something with entity
}
}
rkeazor
04/14/2020, 6:36 AMEllen Spertus
04/14/2020, 8:18 PM