Database 无法使用DAO运行inTransaction或withTransaction
我有一个非常简单的过程,可以从协同程序内的数据库中删除多个项目,如下所示:Database 无法使用DAO运行inTransaction或withTransaction,database,sqlite,kotlin,dao,Database,Sqlite,Kotlin,Dao,我有一个非常简单的过程,可以从协同程序内的数据库中删除多个项目,如下所示: //Removes item from the folder (folderId) val removeItems = Job() val coroutineScope = CoroutineScope(removeItems + Dispatchers.IO) coroutineScope.launch { try { MyDatabase.getInstance(application).
//Removes item from the folder (folderId)
val removeItems = Job()
val coroutineScope = CoroutineScope(removeItems + Dispatchers.IO)
coroutineScope.launch {
try {
MyDatabase.getInstance(application).withTransaction {
Ids.forEach {
database.removeItemFromFolderItem(folderId!!, it)
}
}
} catch (e: Exception) {
//_response.value = "Failure: ${e.message}"
}
}
下面是removeItemFromFolderItem的DAO函数:
@Query("DELETE FROM folders_items_table WHERE folder_name = :folder AND item_id = :item")
suspend fun removeItemFromFolderItem(folder: String, item: String)
由于某种原因,“withTransaction{}”中的块无法执行。我也尝试过使用非挂起DAO函数进行“inTransaction”,但得到了相同的结果,块没有被执行。我想这是getInstance()的一个与“上下文”相关的问题?好的,更多的细节:这个问题似乎有点“随机”,我不知道为什么。如果有人有什么想法,请告诉我!谢谢