public class OrderedRealmCollectionSnapshot<E> extends AbstractList<E>
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.
}
}
});
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Deprecated.
|
void |
add(int index,
E element)
Deprecated.
|
boolean |
addAll(Collection<? extends E> collection)
Deprecated.
|
boolean |
addAll(int location,
Collection<? extends E> collection)
Deprecated.
|
double |
average(String fieldName)
Returns the average of a given field.
|
void |
clear()
Deprecated.
|
boolean |
contains(Object object)
Searches this
OrderedRealmCollection for the specified object. |
OrderedRealmCollectionSnapshot<E> |
createSnapshot()
Creates a snapshot from this
OrderedRealmCollection . |
boolean |
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm.
|
boolean |
deleteFirstFromRealm()
Deletes the first object from the Realm.
|
void |
deleteFromRealm(int location)
Deletes the object at the given index from the Realm.
|
boolean |
deleteLastFromRealm()
Deletes the last object from the Realm.
|
E |
first()
Gets the first object from the collection.
|
E |
first(E defaultValue)
Gets the first object from the collection.
|
OrderedRealmCollection<E> |
freeze()
Returns a frozen snapshot of this collection.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
Realm |
getRealm()
Returns the
Realm instance to which this collection belongs. |
boolean |
isFrozen() |
boolean |
isLoaded()
Checks if a collection has finished loading its data yet.
|
boolean |
isManaged()
A
RealmResults or a OrderedRealmCollectionSnapshot is always a managed collection. |
boolean |
isValid()
Checks if the collection is still valid to use, i.e., the
Realm instance hasn't been closed. |
Iterator<E> |
iterator()
Returns an iterator for the results of a query.
|
E |
last()
Gets the last object from the collection.
|
E |
last(E defaultValue)
Gets the last object from the collection.
|
ListIterator<E> |
listIterator()
Returns a list iterator for the results of a query.
|
ListIterator<E> |
listIterator(int location)
Returns a list iterator on the results of a query.
|
boolean |
load()
Blocks the collection until all data are available.
|
Number |
max(String fieldName)
Finds the maximum value of a field.
|
Date |
maxDate(String fieldName)
Finds the maximum date.
|
Number |
min(String fieldName)
Finds the minimum value of a field.
|
Date |
minDate(String fieldName)
Finds the minimum date.
|
E |
remove(int index)
Deprecated.
|
boolean |
remove(Object object)
Deprecated.
|
boolean |
removeAll(Collection<?> collection)
Deprecated.
|
boolean |
retainAll(Collection<?> collection)
Deprecated.
|
E |
set(int location,
E object)
Deprecated.
|
int |
size()
Returns the number of elements in this query result.
|
RealmResults<E> |
sort(String fieldName)
Not supported by
OrderedRealmCollectionSnapshot . |
RealmResults<E> |
sort(String[] fieldNames,
Sort[] sortOrders)
Not supported by
OrderedRealmCollectionSnapshot . |
RealmResults<E> |
sort(String fieldName,
Sort sortOrder)
Not supported by
OrderedRealmCollectionSnapshot . |
RealmResults<E> |
sort(String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2)
Not supported by
OrderedRealmCollectionSnapshot . |
Number |
sum(String fieldName)
Calculates the sum of a given field.
|
RealmQuery<E> |
where()
Deprecated.
|
equals, hashCode, indexOf, lastIndexOf, subList
containsAll, isEmpty, toArray, toArray, toString
containsAll, equals, hashCode, indexOf, isEmpty, lastIndexOf, replaceAll, sort, spliterator, subList, toArray, toArray
parallelStream, removeIf, stream
public int size()
public RealmResults<E> sort(String fieldName)
OrderedRealmCollectionSnapshot
. Use 'sort()' on the original
OrderedRealmCollection
instead.sort
in interface OrderedRealmCollection<E>
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date,
and String are supported.RealmResults
will be created and returned. The original collection stays unchanged.UnsupportedOperationException
public RealmResults<E> sort(String fieldName, Sort sortOrder)
OrderedRealmCollectionSnapshot
. Use 'sort()' on the original
OrderedRealmCollection
instead.sort
in interface OrderedRealmCollection<E>
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date,
and String are supported.sortOrder
- the direction to sort by.RealmResults
will be created and returned. The original collection stays unchanged.UnsupportedOperationException
public RealmResults<E> sort(String fieldName1, Sort sortOrder1, String fieldName2, Sort sortOrder2)
OrderedRealmCollectionSnapshot
. Use 'sort()' on the original
OrderedRealmCollection
instead.sort
in interface OrderedRealmCollection<E>
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
- second field name. Only fields of type boolean, short, int, long, float,
double, Date, and String are supported.sortOrder2
- sort order for second field.RealmResults
will be created and returned. The original collection stays unchanged.UnsupportedOperationException
public RealmResults<E> sort(String[] fieldNames, Sort[] sortOrders)
OrderedRealmCollectionSnapshot
. Use 'sort()' on the original
OrderedRealmCollection
instead.sort
in interface OrderedRealmCollection<E>
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.RealmResults
will be created and returned. The original collection stays unchanged.UnsupportedOperationException
@Deprecated public RealmQuery<E> where()
OrderedRealmCollectionSnapshot
. Use 'where()' on the original
OrderedRealmCollection
instead.UnsupportedOperationException
RealmQuery
public boolean isLoaded()
true
if data has been loaded and is available, false
if data is still being loaded.public boolean load()
true
if the data could be successfully loaded, false
otherwise.public OrderedRealmCollectionSnapshot<E> createSnapshot()
OrderedRealmCollection
.createSnapshot
in interface OrderedRealmCollection<E>
OrderedRealmCollectionSnapshot
public OrderedRealmCollection<E> freeze()
RealmCollection
IllegalStateException
.
Freezing a collection also creates a Realm which has its own lifecycle, but if the live Realm that spawned the original collection is fully closed (i.e. all instances across all threads are closed), the frozen Realm and this collection will be closed as well.
Frozen collections can be queried as normal, but trying to mutate it in any way or attempting to register a listener will
throw an IllegalStateException
.
Note: Keeping a large number of frozen collections with different versions alive can have a negative impact on the filesize
of the Realm. In order to avoid such a situation, it is possible to set RealmConfiguration.Builder.maxNumberOfActiveVersions(long)
.
public void deleteFromRealm(int location)
deleteFromRealm
in interface OrderedRealmCollection<E>
location
- the array index identifying the object to be removed.IndexOutOfBoundsException
- if location < 0 || location >= size()
.IllegalStateException
- if the Realm is closed or the method is called from the wrong thread.public boolean deleteFirstFromRealm()
deleteFirstFromRealm
in interface OrderedRealmCollection<E>
true
if an object was deleted, false
otherwise.IllegalStateException
- if the Realm is closed or the method is called on the wrong thread.public boolean deleteLastFromRealm()
deleteLastFromRealm
in interface OrderedRealmCollection<E>
true
if an object was deleted, false
otherwise.IllegalStateException
- if the Realm is closed or the method is called from the wrong thread.public boolean deleteAllFromRealm()
deleteAllFromRealm
in interface RealmCollection<E>
true
if objects was deleted, false
otherwise.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.public boolean isFrozen()
public boolean isValid()
Realm
instance hasn't been closed. It
will always return true
for an unmanaged collection.isValid
in interface RealmCollection<E>
true
if it is still valid to use or an unmanaged collection, false
otherwise.public boolean isManaged()
RealmResults
or a OrderedRealmCollectionSnapshot
is always a managed collection.isManaged
in interface RealmCollection<E>
true
.RealmCollection.isManaged()
public boolean contains(Object object)
OrderedRealmCollection
for the specified object.contains
in interface RealmCollection<E>
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
object
- the object to search for.true
if object
is an element of this OrderedRealmCollection
,
false
otherwise.public E get(int location)
get
in interface List<E>
get
in class AbstractList<E>
location
- the index of the element to return.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public E first()
first
in interface OrderedRealmCollection<E>
public E first(E defaultValue)
first
in interface OrderedRealmCollection<E>
public E last()
last
in interface OrderedRealmCollection<E>
public E last(E defaultValue)
last
in interface OrderedRealmCollection<E>
public Iterator<E> iterator()
ConcurrentModificationException
if accessed.iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in interface List<E>
iterator
in class AbstractList<E>
Iterator
public ListIterator<E> listIterator()
ConcurrentModificationException
if accessed.listIterator
in interface List<E>
listIterator
in class AbstractList<E>
ListIterator
public ListIterator<E> listIterator(int location)
ConcurrentModificationException
if accessed.listIterator
in interface List<E>
listIterator
in class AbstractList<E>
location
- the index at which to start the iteration.IndexOutOfBoundsException
- if location < 0 || location > size()
.ListIterator
public Number min(String fieldName)
min
in interface RealmCollection<E>
fieldName
- the field to look for a minimum on. Only number fields are supported.null
as the value for the given field, null
will be
returned. Otherwise the minimum value is returned. When determining the minimum value, objects with null
values are ignored.public Date minDate(String fieldName)
minDate
in interface RealmCollection<E>
fieldName
- the field to look for the minimum date. If fieldName is not of Date type, an exception is
thrown.null
as the value for the given date field, null
will be returned. Otherwise the minimum date is returned. When determining the minimum date, objects with
null
values are ignored.public Number max(String fieldName)
max
in interface RealmCollection<E>
fieldName
- the field to look for a maximum on. Only number fields are supported.null
as the value for the given field, null
will be
returned. Otherwise the maximum value is returned. When determining the maximum value, objects with null
values are ignored.public Date maxDate(String fieldName)
maxDate
in interface RealmCollection<E>
fieldName
- the field to look for the maximum date. If fieldName is not of Date type, an exception is
thrown.null
as the value for the given date field, null
will be returned. Otherwise the maximum date is returned. When determining the maximum date, objects with
null
values are ignored.IllegalArgumentException
- if fieldName is not a Date field.public Number sum(String fieldName)
sum
in interface RealmCollection<E>
fieldName
- the field to sum. Only number fields are supported.null
as the value for the given field, 0
will be returned. When computing the sum, objects with null
values are ignored.public double average(String fieldName)
average
in interface RealmCollection<E>
fieldName
- the field to calculate average on. Only number fields are supported.null
as the value for the given field,
0
will be returned. When computing the average, objects with null
values are ignored.@Deprecated public E remove(int index)
RealmResults
and OrderedRealmCollectionSnapshot
.remove
in interface List<E>
remove
in class AbstractList<E>
UnsupportedOperationException
@Deprecated public boolean remove(Object object)
RealmResults
and OrderedRealmCollectionSnapshot
.remove
in interface Collection<E>
remove
in interface List<E>
remove
in class AbstractCollection<E>
UnsupportedOperationException
@Deprecated public boolean removeAll(Collection<?> collection)
RealmResults
and OrderedRealmCollectionSnapshot
.removeAll
in interface Collection<E>
removeAll
in interface List<E>
removeAll
in class AbstractCollection<E>
UnsupportedOperationException
@Deprecated public E set(int location, E object)
RealmResults
and OrderedRealmCollectionSnapshot
.set
in interface List<E>
set
in class AbstractList<E>
UnsupportedOperationException
@Deprecated public boolean retainAll(Collection<?> collection)
RealmResults
and OrderedRealmCollectionSnapshot
.retainAll
in interface Collection<E>
retainAll
in interface List<E>
retainAll
in class AbstractCollection<E>
UnsupportedOperationException
@Deprecated public void clear()
RealmResults
and OrderedRealmCollectionSnapshot
.clear
in interface Collection<E>
clear
in interface List<E>
clear
in class AbstractList<E>
UnsupportedOperationException
- always.@Deprecated public boolean add(E element)
RealmResults
and OrderedRealmCollectionSnapshot
.add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
UnsupportedOperationException
- always.@Deprecated public void add(int index, E element)
RealmResults
and OrderedRealmCollectionSnapshot
.add
in interface List<E>
add
in class AbstractList<E>
UnsupportedOperationException
- always.@Deprecated public boolean addAll(int location, Collection<? extends E> collection)
RealmResults
and OrderedRealmCollectionSnapshot
.addAll
in interface List<E>
addAll
in class AbstractList<E>
UnsupportedOperationException
- always.@Deprecated public boolean addAll(Collection<? extends E> collection)
RealmResults
and OrderedRealmCollectionSnapshot
.addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
UnsupportedOperationException
- always.public Realm getRealm()
Realm
instance to which this collection belongs.
Calling BaseRealm.close()
on the returned instance is discouraged as it is the same as
calling it on the original Realm instance which may cause the Realm to fully close invalidating the
query result.
Realm
instance this collection belongs to.IllegalStateException
- if the Realm is an instance of DynamicRealm
or the
Realm
was already closed.