如何解决在runInTransaction块中的suspend方法
我使用以下代码出现编译错误:
只能在协程体内调用悬浮函数
有人可以向我解释为什么吗?我需要做些什么才能使其工作(不使用@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()
}
预先感谢您的帮助
解决方法
对于suspend
函数,请使用withTransaction
而不是runInTransaction
我建议您阅读https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html上的《协程》指南
要回答您的问题,您需要设置一个协程作用域和一个用于使协程运行的调度程序线程。最简单的是这样的:
GlobalScope.launch(Dispatchers.IO) {
replaceAccounts(newAccounts)
}
它将在IO线程(处理IO任务的主线程之外的线程)上的GlobalScope(协程的“生命周期”绑定到整个应用程序的生命周期)中运行协程。
编辑
我喜欢@ IR42的答案。在此基础上,使用withTransaction
可以让Room处理执行数据库操作的线程,并有助于限制数据库的并发性。
GlobalScope.launch(Dispatchers.Main) {
replaceAccounts(newAccounts)
}
override suspend fun replaceAccounts(newAccounts: List<Account>) {
database.withTransaction {
database.accountDao().deleteAllAccounts() // I have the error on this line
database.accountDao().insertAccounts(newAccounts) // Here too
}
}
通过Room自己的文章之一查看有关此文章的更多信息:https://medium.com/androiddevelopers/threading-models-in-coroutines-and-android-sqlite-api-6cab11f7eb90
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。