Package io.realm

Class OrderedRealmCollectionSnapshot<E>

  • All Implemented Interfaces:
    io.realm.internal.Freezable<RealmCollection<E>>, io.realm.internal.ManageableObject, OrderedRealmCollection<E>, RealmCollection<E>, Iterable<E>, Collection<E>, List<E>

    public class OrderedRealmCollectionSnapshot<E>
    extends AbstractList<E>
    An OrderedRealmCollectionSnapshot is a special type of OrderedRealmCollection. It can be created by calling OrderedRealmCollection.createSnapshot(). Unlike RealmResults and RealmList, its size and order of elements will never be changed after creation.

    OrderedRealmCollectionSnapshot is useful when making changes which may impact the size or order of the collection in simple loops. For example:

     
     final RealmResults<Dog>  dogs = realm.where(Dog.class).findAll();
     final OrderedRealmCollectionSnapshot<Dog> snapshot = dogs.createSnapshot();
     final int dogsCount = snapshot.size(); // dogs.size() == snapshot.size() == 10
     realm.executeTransaction(new Realm.Transaction() {
         /@Override
         public void execute(Realm realm) {
             for (int i = 0; i < dogsCount; i++) {
             // This won't work since RealmResults is always up-to-date, its size gets decreased by 1 after every loop. An
             // IndexOutOfBoundsException will be thrown after 5 loops.
             // dogs.deleteFromRealm(i);
             snapshot.deleteFromRealm(i); // Deletion on OrderedRealmCollectionSnapshot won't change the size of it.
             }
         }
     });