小编Lou*_*uis的帖子

runInTransaction 块中的挂起方法

使用以下代码时出现编译错误:

暂停函数只能在协程体内调用

有人可以向我解释为什么吗?我需要做什么才能使它工作(不使用@Transaction注释)?

override suspend fun replaceAccounts(newAccounts: List<Account>) {
    database.runInTransaction {
        database.accountDao().deleteAllAccounts() // I have the error on this line
        database.accountDao().insertAccounts(newAccounts) // Here too
    }
}

@Dao
abstract class AccountDao : BaseDao<AccountEntity> {

    @Query("DELETE FROM Account")
    abstract suspend fun deleteAllAccounts()

}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

android coroutine kotlin kotlin-coroutines

9
推荐指数
1
解决办法
949
查看次数

标签 统计

android ×1

coroutine ×1

kotlin ×1

kotlin-coroutines ×1