Scope: Iterating a RealmObject and clearing the ArrayList field

I have RealmResults<Section>one that has a field RealmList<Event>that I want to clear for each section.

I tried (insude mRealm.executeTransaction)

for (Section section : mSections) {
    section.getEvents().clear();
}

and

Iterator<Section> sectionIterator = mSections.iterator();
while (sectionIterator.hasNext()) {
    sectionIterator.next().getEvents().clear();
}

but realm throws this exception

java.util.ConcurrentModificationException: no external changes in the kingdom are allowed when RealmResults are repeated. using iterators.

+4
source share
1 answer

Since you are not actually deleting the elements you are executing, you can simply use the traditional loop:

for (int i = 0; i < mSections.size(); i++) {
    mSections.get(i).getEvents().clear();
}

: Iterator, remove() Iterator.

.

+7

Source: https://habr.com/ru/post/1584986/


All Articles