迭代结果时无法修改Realm对象

时间:2015-11-28 21:09:46

标签: android realm

领域在更新ConcurrentModificationException课程时抛出Model

java.util.ConcurrentModificationException: No outside changes to a Realm is allowed while iterating a RealmResults. Use iterators methods instead.

这是我的代码 -

Realm realm = Realm.getDefaultInstance();
RealmQuery<Model> query = realm.where(Model.class);
query.equalTo("decision", getString(android.R.string.yes));
ListIterator<Model> listIterator = query.findAll().listIterator();
realm.beginTransaction();
while (listIterator.hasNext()) {
    Model model = listIterator.next();
    model.setDecision(getString(android.R.string.no));
}
realm.commitTransaction();

1 个答案:

答案 0 :(得分:2)

只需使用正常循环

realm.beginTransaction();
int size = results.size();
for (int i = size-1; i >= 0; i--) {
  results.get(i).setDecision(getString(android.R.string.no));
}
realm.commitTransaction();