安装包 io.realm

OrderedRealmCollectionSnapshot<E> 类

  • 所有已实现的接口:
    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>
    OrderedRealmCollectionSnapshot是一种特殊类型的OrderedRealmCollection 。 它可以通过调用OrderedRealmCollection.createSnapshot()创建。 与RealmResultsRealmList不同,其大小和元素顺序在创建后永远不会改变。

    OrderedRealmCollectionSnapshot 在进行可能影响简单循环中集合的大小或顺序的更改时非常有用。 例如:

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