runInTransaction 块中的挂起方法

Lou*_*uis 9 android coroutine kotlin kotlin-coroutines

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

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

有人可以向我解释为什么吗?我需要做什么才能使它工作(不使用@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)

在此先感谢您的帮助

IR4*_*R42 21

对于suspend您应该使用的功能withTransaction而不是runInTransaction