"Cannot access database on the main thread since it may potentially lock the UI for a long period of time" 启动协程时出错

"Cannot access database on the main thread since it may potentially lock the UI for a long period of time" error on lauching coroutine

我收到此错误:无法在主线程上访问数据库,因为它可能会长时间锁定 UI。 当我在 ViewModel(下面的代码)中启动有趣的 turnAllWordsOn() 时,就会发生这种情况。这个函数启动协程,我认为协程总是在后台线程上工作。那么为什么我会收到此错误? 感谢任何帮助

在片段中:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.action_turn_all_words_on -> {
            viewModel.turnAllWordsOn()
            true
        }
    // othes items
    }

在视图模型中:

fun turnAllWordsOn() = viewModelScope.launch {
    wordDao.turnAllWordsOn()
}

在道中:

@Query("UPDATE word_table SET shown = 1")
fun turnAllWordsOn()

如果你想让 Room 在后台线程上 运行 它,你必须将你的 Dao 函数标记为挂起函数。否则你所做的就是从协程范围调用同步函数。

@Query("UPDATE word_table SET shown = 1")
suspend fun turnAllWordsOn()

附带说明一下,挂起函数不会在后台线程上自动 运行,但是当您将查询标记为挂起时,Room 会在幕后完成必要的工作。

即使你有答案,我还是想给出一些解决方法和问题的原因, 进行联网,或者访问数据库会阻塞Ui个线程,所以你可以使用

  1. 使用 RxJava:

    Completable.fromAction { wordDao.turnAllWordsOn() }

  2. 使用协程:

    @Query("UPDATE word_table SET shown = 1") 暂停乐趣 turnAllWordsOn()