不允许嵌套交易

时间:2018-11-07 07:26:02

标签: android realm

如何从领域中删除多个对象?

realm.beginTransaction
realm.executeTransaction(realm -> {
        for(String id : listOfIds) {

         realm.where(StorageClass.class).equalTo("id", id).findFirst().removeFromRealm();
        }
      }
  );
realm.commitTransaction();

如果我执行上述操作,并尝试从现有ID列表中删除所有ID,则会出现以下错误:

java.lang.IllegalStateException: Nested transactions are not allowed. Use commitTransaction() after each beginTransaction().
    at io.realm.internal.ImplicitTransaction.promoteToWrite(ImplicitTransaction.java:39)
    at io.realm.Realm.beginTransaction(Realm.java:1435)

预先感谢

1 个答案:

答案 0 :(得分:0)

只需删除realm.beginTransaction()和realm.commitTransaction(),因为内部执行事务将调用begin和commit事务。