I am getting this error while trying to display Room data in a LazyColumn in my project.
Cannot access database on the main thread since it may potentially lock the UI for a long period of time
I don't know why it is trying to access the database since I'm getting it with a ViewModelScope. Bellow is my MainActivity code and the ViewModel
@AndroidEntryPoint class MainActivity : AppCompatActivity() { private val viewModel: UserViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val users = viewModel.userList.value LazyColumn(modifier = Modifier.fillMaxWidth()) { items(users){data-> MyCard(data) } @HiltViewModel class UserViewModel @Inject constructor( private val repository: MainRepository ) : ViewModel() { val userList: MutableState<List<User>> = mutableStateOf(listOf()) init { viewModelScope.launch { try { val result: List<User> = repository.getAllUsers() userList.value = result } catch (e: Exception) { Log.e("SSS", "${e.message.toString()}; ${e.stackTrace}") } } } https://stackoverflow.com/questions/66701263/why-is-it-trying-to-access-the-database-on-the-main-thread March 19, 2021 at 09:11AM
没有评论:
发表评论