简体   繁体   English

Android Room 数据库事务

[英]Android Room database transactions

With the new Room Database in Android, I have a requirement where there are two sequential operations that needs to be made:使用 Android 中的新房间数据库,我有一个要求,需要进行两个顺序操作:

removeRows(ids);
insertRows(ids);

If I run this, I see (on examining the db) that there are some rows missing - I assume they are being deleted after inserting.如果我运行它,我会看到(在检查数据库时)缺少一些行 - 我假设它们在插入后被删除。 viz.即。 the first operation is running in parallel to the second.第一个操作与第二个操作并行运行。

If I use a transaction block, such as this, then it's all fine - the first operation seems to complete before doing the second:如果我使用事务块,例如这样,那么一切都很好 - 第一个操作似乎在执行第二个操作之前完成:

roomDb.beginTransaction();
removeRows(ids);
roomDb.endTransaction();

insertRows(ids);

It's also fine if I give a sleep in-between instead:如果我在中间睡一觉也没关系:

removeRows(ids);
Thread.sleep(500);

insertRows(ids);

There doesn't seem to be much documentation for Room, and was wondering if I should use the transaction block like the above when I have sequential operations to be done, or is there any better way of doing it. Room 似乎没有太多文档,我想知道当我要完成顺序操作时是否应该使用上面的事务块,或者有没有更好的方法来做到这一点。

EDIT : After @CommonsWare pointed out, @Query are asynchronous, while @Insert and @Delete are synchronous.编辑:@CommonsWare 指出后, @Query是异步的,而@Insert@Delete是同步的。 In view of this, how would I get a query which deletes rows to be async:鉴于此,我将如何获得删除行以进行异步的查询:

@Query("DELETE from table WHERE id IN(:ids)")
int removeRows(List<Long> ids);

According to the build output I get Deletion methods must either return void or return int (the number of deleted rows) , if I try to wrap the return type in a Flowable .根据构建输出,如果我尝试将返回类型包装在Flowable ,则Deletion methods must either return void or return int (the number of deleted rows)

As pointed out on documentation for Transaction , you can do following:正如Transaction文档中所指出的,您可以执行以下操作:

 @Dao
 public abstract class ProductDao {
    @Insert
    public abstract void insert(Product product);

    @Delete
    public abstract void delete(Product product);

    @Transaction
    public void insertAndDeleteInTransaction(Product newProduct, Product oldProduct) {
         // Anything inside this method runs in a single transaction.
         insert(newProduct);
         delete(oldProduct);
     }
 }
 

As @CommonsWare pointed out, @Query are asynchronous , while @Insert , @Delete , @Update are synchronous.正如@CommonsWare 指出的那样,@Query 是异步的,而@Insert、@Delete、@Update 是同步的。

If you want to execute multiple queries in single transaction , Room also provides a method for that as mentioned below.如果您想在单个事务中执行多个查询,Room 还提供了一种方法,如下所述。

roomDB.runInTransaction(new Runnable() {
        @Override
        public void run() {
            removeRows(ids);
            insertRows(ids);
        }
    });

I hope this will solve your problem.我希望这能解决你的问题。

For Room transactions in Kotlin you can use:对于 Kotlin 中的房间事务,您可以使用:

  • Interface with implemented method , like:与已实现方法的接口,例如:
@Dao 
interface Dao {

    @Insert 
    fun insert(item: Item)

    @Delete 
    fun delete(item: Item)

    @Transaction
    fun replace(oldItem: Item, newItem: Item){
        delete(oldItem)
        insert(newItem)
    }

}
  • Or use open function, like:或者使用open函数,例如:
@Dao 
abstract class Dao {

    @Insert 
    abstract fun insert(item: Item)

    @Delete 
    abstract fun delete(item: Item)

    @Transaction
    open fun replace(oldItem: Item, newItem: Item){
        delete(oldItem)
        insert(newItem)
    }

}

You'll get error: Method annotated with @Transaction must not be private, final, or abstract.你会得到error: Method annotated with @Transaction must not be private, final, or abstract. without open modifier.没有打开修饰符。

I believe when we are using DAO interfaces, still we can perform transaction using default interface methods.我相信当我们使用 DAO 接口时,我们仍然可以使用默认的接口方法执行事务。 We need to add the annotation @JvmDefault and @Transaction and we can perform any operation inside that, which belong to single transaction.我们需要添加注解@JvmDefault 和@Transaction,我们可以在其中执行任何属于单个事务的操作。

@Dao
interface TestDao {
    @Insert
    fun insert(dataObj: DataType)

    @Update
    fun update(dataObj: DataType): Completable

    @Delete
    fun delete(dataObj: DataType): Completable

    @Query("DELETE FROM $TABLE_NAME")
    fun deleteAllData()

    @Query("SELECT * FROM $TABLE_NAME ORDER BY id DESC")
    fun getAllData(): Single<List<DataType>>

    @JvmDefault
    @Transaction
    fun singleTransaction(dataList: List<DataType>) {
        deleteAllData()
        dataList.forEach {
            insert(it)
        }
    }
}

here's the solution to this problem:这是这个问题的解决方案:

@Query("SELECT * FROM friend WHERE id = :id")
Friend getFriendByID(int id);
@Delete
void delete(Friend friend);

Friend friendToBeDeleted = friendDAO.getFriendByID(id);
friendDAO.delete(friendToBeDeleted);

You have to go through two steps!你必须经过两个步骤!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM