室:使用@Transaction时收到错误 [英] Room: Receiving error when using @Transaction

查看:106
本文介绍了室:使用@Transaction时收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DAO类中有一个以@Transaction注释的方法,这会导致以下错误:

I'm have a method annotated with @Transaction in my DAO class, which is causing the following error:

DAO方法只能使用以下一项进行注释:插入,删除,查询,更新

A DAO method can be annotated with only one of the following:Insert,Delete,Query,Update

这是我的课程:

@Dao interface Dao {

    @Insert(onConflict = REPLACE) fun insertList(chacaras: List<String>)

    @Query("SELECT * FROM chacara WHERE cityId = :cityId")
    fun getListOfCity(cityId: String): LiveData<List<String>>

    @Delete fun deleteList(chacaraList: List<String>)

    @Transaction
    fun updateList(list: List<String>){
        deleteList(list)
        insertList(list)
    }

}

当我删除使用@Transaction注释的方法时,它将正常编译. 反正有解决此问题的方法吗?

When I remove the method annotated with @Transaction it compiles normally. Is there anyway to fix this?

推荐答案

根据交易文档

抽象Dao类中的方法标记为事务处理方法.

Marks a method in an abstract Dao class as a transaction method.

将您的班级更改为:

@Dao abstract class Dao {

    @Insert(onConflict = REPLACE) abstract fun insertList(chacaras: List<String>)

    @Query("SELECT * FROM chacara WHERE cityId = :cityId")
    abstract fun getListOfCity(cityId: String): LiveData<List<String>>

    @Delete abstract fun deleteList(chacaraList: List<String>)

    @Transaction
    open fun updateList(list: List<String>){
        deleteList(list)
        insertList(list)
    }

}

这篇关于室:使用@Transaction时收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆