Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
io.realm

Interfaz OrderedRealmCollection

Implemented interfaces:

  • java.util.List

  • io.realm.RealmCollection

  • java.util.Collection

  • java.lang.Iterable

  • java.util.Collection

  • java.lang.Iterable

An OrderedRealmCollection Es una colección que mantiene un orden para sus elementos. Cada elemento en OrderedRealmCollection tiene un índice. Por lo tanto, se puede acceder a cada elemento por su índice, siendo el primero cero. Normalmente, los OrderedRealmCollection permiten elementos duplicados, a diferencia de los conjuntos, donde los elementos deben ser únicos.

Hay tres tipos de OrderedRealmCollection. RealmResults y RealmList son colecciones activas. Se actualizan constantemente y nunca contendrán un RealmObject no válido. OrderedRealmCollectionSnapshot es diferente. Una OrderedRealmCollectionSnapshot se puede crear a partir de otra OrderedRealmCollection. Su tamaño y el orden de los elementos se mantienen iguales a los de la colección original al crearse. OrderedRealmCollectionSnapshot puede contener RealmObject no válidos si los objetos se eliminan.

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

Como puedes ver, después de la eliminación, el tamaño y el orden de los elementos del snapshot se mantienen igual que antes. Pero el elemento en la posición se vuelve inválido.

Modificador y Tipo
Método y descripción

Creates a snapshot from this OrderedRealmCollection .

public booleano

Deletes the first object from the Realm.

public void

int location
)

Deletes the object at the given index from the Realm.

public booleano

Elimina el último objeto del Reino.

public E

E defaultValue
)

Gets the first object from the collection.

public E

Gets the first object from the collection.

public E

E defaultValue
)

Gets the last object from the collection.

public E

last ()

Gets the last object from the collection.

java.lang.String[] fieldNames,
io.realm.Sort[] sortOrders
)

Ordena una colección en función de los campos y órdenes de clasificación enviados.

String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2
)

Ordena una colección en función de los campos y órdenes de clasificación enviados.

String fieldName,
Sort sortOrder
)

Sorts a collection based on the provided field and sort order.

String fieldName
)

Ordena una colección según el campo proporcionado en orden ascendente.

Creates a snapshot from this OrderedRealmCollection .

Devuelve

the snapshot of this collection.

Throws

Tip

public boolean deleteFirstFromRealm ()

Deletes the first object from the Realm. This also removes it from this collection.

Devuelve

true if an object was deleted, false otherwise.

Throws

public void deleteFromRealm (
int location
)

Deletes the object at the given index from the Realm. This also removes it from the collection.

Parámetros

  • location - el índice del arreglo que identifica el objeto que se va a eliminar.

Throws

public boolean deleteLastFromRealm ()

Elimina el último objeto del dominio. Esto también lo elimina de esta colección.

Devuelve

true if an object was deleted, false otherwise.

Throws

public E first (
E defaultValue
)

Gets the first object from the collection. If the collection is empty, the provided default will be used instead.

Devuelve

the first object or the provided default.

public E first ()

Gets the first object from the collection.

Devuelve

the first object.

Throws

public E last (
E defaultValue
)

Gets the last object from the collection. If the collection is empty, the provided default will be used instead.

Devuelve

the last object or the provided default.

public E last ()

Gets the last object from the collection.

Devuelve

the last object.

Throws

java.lang.String[] fieldNames,
io.realm.Sort[] sortOrders
)

Ordena una colección en función de los campos y órdenes de clasificación enviados.

Parámetros

  • fieldNames - an array of field names to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.

  • sortOrders - the directions to sort by.

Devuelve

se creará y devolverá un nuevo RealmResults ordenado. La colección original permanece sin cambios.

Throws

String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2
)

Ordena una colección en función de los campos y órdenes de clasificación enviados.

Parámetros

  • fieldName1 - first field name. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.

  • sortOrder1 - sort order for first field.

  • fieldName2 - Nombre del segundo campo. Solo se admiten campos de tipo booleano, corto, int, largo, flotante, doble, fecha y cadena.

  • sortOrder2 - orden de clasificación para el segundo campo.

Devuelve

se creará y devolverá un nuevo RealmResults ordenado. La colección original permanece sin cambios.

Throws

String fieldName,
Sort sortOrder
)

Sorts a collection based on the provided field and sort order.

Parámetros

  • fieldName - the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.

  • sortOrder - la dirección por la que ordenar.

Devuelve

se creará y devolverá un nuevo RealmResults ordenado. La colección original permanece sin cambios.

Throws

String fieldName
)

Ordena una colección según el campo proporcionado en orden ascendente.

Parámetros

  • fieldName - the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.

Devuelve

se creará y devolverá un nuevo RealmResults ordenado. La colección original permanece sin cambios.

Throws

Volver

OrderedCollectionChangeSet.estado

En esta página