Android Room database transactions

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

With the new Room Database in Android, I have a requirement where there are two sequential operations that needs to be made:

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.

EDIT: After @CommonsWare pointed out, @Query are asynchronous, while @Insert and @Delete are synchronous. 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.

回答1:

as pointed out on documentation for Transaction, you can do following:

 @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);      }  } 


回答2:

As @CommonsWare pointed out, @Query are asynchronous , while @Insert , @Delete , @Update are synchronous.

If you want to execute multiple queries in single transaction , Room also provides a method for that as mentioned below.

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

I hope this will solve your problem.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!