Interface OrderedRealmCollection
On this page
io.realm
Implemented interfaces:
An OrderedRealmCollection
is a collection which maintains an ordering for its elements. Every element in the OrderedRealmCollection
has an index. Each element can thus be accessed by its index, with the first index being zero. Normally, OrderedRealmCollection
s allow duplicate elements, as compared to Sets, where elements have to be unique.
There are three types of OrderedRealmCollection . RealmResults and RealmList are live collections. They are up-to-date all the time and they will never contain an invalid RealmObject . OrderedRealmCollectionSnapshot is different. An OrderedRealmCollectionSnapshot can be created from another OrderedRealmCollection . Its size and elements order stay the same as the original collection's when it was created. OrderedRealmCollectionSnapshot may contain invalid RealmObject s if the objects get deleted.
Using iterators to iterate on OrderedRealmCollection will always work. You can delete or modify the elements without impacting the iterator. See below example:
RealmResults<Dog> dogs = realm.where(Dog.class).findAll(); int s = dogs.size(); // 10 realm.beginTransaction(); for (Dog dog : dogs) { dog.deleteFromRealm(); s = dogs.size(); // This will be decreased by 1 every time after a dog is removed. } realm.commitTransaction(); s = dogs.size(); // 0
An iterator created from a live collection will create a stable view when the iterator is created, allowing you to delete and modify elements while iterating without impacting the iterator. However, the RealmResults
backing the iterator will still be live updated meaning that size and order of elements can change when iterating. RealmList has the same behaviour as RealmResults since they are both live collections.
A simple for-loop is different. See below example:
RealmResults<Dog> dogs = realm.where(Dog.class).findAll(); realm.beginTransaction(); for (int i = 0; i < dogs.size(); i++) { dogs.get(i).deleteFromRealm(); } realm.commitTransaction(); s = dogs.size(); // 5
The above example only deletes half of elements in the RealmResults . This is because of dogs.size()
decreased by 1 for every loop. The deletion happens in the loop will immediately impact the size of RealmResults
. To solve this problem, you can create a OrderedRealmCollectionSnapshot from the RealmResults or RealmList and do simple for-loop on that instead:
RealmResults<Dog> dogs = realm.where(Dog.class).findAll(); OrderedRealmCollectionSnapshot snapshot = dogs.createSnapshot(); // dogs.size() == 10 && snapshot.size() == 10 realm.beginTransaction(); for (int i = 0; i < snapshot.size(); i++) { snapshot.get(0).deleteFromRealm(); // snapshot.get(0).isValid() == false } realm.commitTransaction(); // dogs.size() == 0 && snapshot.size() == 10
As you can see, after deletion, the size and elements order of snapshot stay the same as before. But the element at the position becomes invalid.
Method Summary
Modifier and Type | Method and Description |
---|---|
Creates a snapshot from this OrderedRealmCollection . | |
public boolean | Deletes the first object from the Realm. |
public void | Deletes the object at the given index from the Realm. |
public boolean | Deletes the last object from the Realm. |
public E | Gets the first object from the collection. |
public E | first () Gets the first object from the collection. |
public E | Gets the last object from the collection. |
public E | last () Gets the last object from the collection. |
public RealmResults | Sorts a collection based on the provided fields and sort orders. |
public RealmResults | |
public RealmResults | |
public RealmResults |
Inherited Methods
Method Detail
createSnapshot
public OrderedRealmCollectionSnapshot createSnapshot () |
---|
Creates a snapshot from this OrderedRealmCollection . Returns the snapshot of this collection. Throws
|
deleteFirstFromRealm
public boolean deleteFirstFromRealm () |
---|
Deletes the first object from the Realm. This also removes it from this collection. Returns
Throws
|
deleteFromRealm
Deletes the object at the given index from the Realm. This also removes it from the collection. Parameters
Throws
|
deleteLastFromRealm
public boolean deleteLastFromRealm () |
---|
Deletes the last object from the Realm. This also removes it from this collection. Returns
Throws
|
first
Gets the first object from the collection. If the collection is empty, the provided default will be used instead. Returns the first object or the provided default. |
public E first () |
---|
Gets the first object from the collection. Returns the first object. Throws
|
last
Gets the last object from the collection. If the collection is empty, the provided default will be used instead. Returns the last object or the provided default. |
public E last () |
---|
Gets the last object from the collection. Returns the last object. Throws
|
sort
Sorts a collection based on the provided fields and sort orders. Parameters
Returns a new sorted RealmResults will be created and returned. The original collection stays unchanged. Throws
|
Sorts a collection based on the provided fields and sort orders. Parameters
Returns a new sorted RealmResults will be created and returned. The original collection stays unchanged. Throws
|
Sorts a collection based on the provided field and sort order. Parameters
Returns a new sorted RealmResults will be created and returned. The original collection stays unchanged. Throws
|
Sorts a collection based on the provided field in ascending order. Parameters
Returns a new sorted RealmResults will be created and returned. The original collection stays unchanged. Throws
|