E - The class of objects in this listpublic final class RealmResults<E extends RealmObject>
extends java.util.AbstractList<E>
RealmQuery for a given Realm. The objects
are not copied from the Realm to the RealmResults list, but are just referenced from the
RealmResult instead. This saves memory and increases speed.
RealmResults are live views, which means that if it is on an Looper thread,
it will automatically update its query results after a transaction has been committed. If on a
non-looper thread, BaseRealm.refresh() must be called to update the results.
Updates to RealmObjects from a RealmResults list must be done from within a transaction and the modified objects are persisted to the Realm file during the commit of the transaction.
A RealmResults object cannot be passed between different threads.
Notice that a RealmResults is never null not even in the case where it contains no objects. You
should always use the size() method to check if a RealmResults is empty or not.
RealmQuery.findAll(),
Realm.allObjects(Class),
BaseRealm.beginTransaction()| Modifier and Type | Field and Description |
|---|---|
static boolean |
SORT_ORDER_ASCENDING |
static boolean |
SORT_ORDER_DESCENDING |
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E element)
Deprecated.
|
void |
add(int index,
E element)
Deprecated.
|
void |
addChangeListener(RealmChangeListener listener)
Add a change listener to this RealmResults.
|
double |
average(java.lang.String fieldName)
Returns the average of a given field.
|
void |
clear()
Removes all objects from the list.
|
E |
first()
Get the first object from the list.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
int |
indexOf(java.lang.Object o)
This method is not supported.
|
boolean |
isLoaded()
Returns
true if the results are not yet loaded, false if they are
still loading. |
boolean |
isValid()
Check if
RealmResults is still valid to use i.e. |
java.util.Iterator<E> |
iterator()
Returns an iterator for the results of a query.
|
E |
last()
Get the last object from the list.
|
java.util.ListIterator<E> |
listIterator()
Returns a list iterator for the results of a query.
|
java.util.ListIterator<E> |
listIterator(int location)
Returns a list iterator on the results of a query.
|
boolean |
load()
Make an asynchronous query blocking.
|
java.lang.Number |
max(java.lang.String fieldName)
Find the maximum value of a field.
|
java.util.Date |
maxDate(java.lang.String fieldName)
Find the maximum date.
|
java.lang.Number |
min(java.lang.String fieldName)
Find the minimum value of a field.
|
java.util.Date |
minDate(java.lang.String fieldName)
Find the minimum date.
|
E |
remove(int index)
Removes an object at a given index.
|
void |
removeChangeListener(RealmChangeListener listener)
Remove a previously registered listener.
|
void |
removeChangeListeners()
Remove all registered listeners.
|
void |
removeLast()
Removes and returns the last object in the list.
|
int |
size()
Returns the number of elements in this query result.
|
void |
sort(java.lang.String fieldName)
Sort (ascending) an existing
RealmResults. |
void |
sort(java.lang.String[] fieldNames,
boolean[] sortAscending)
Sort existing
RealmResults. |
void |
sort(java.lang.String fieldName,
boolean sortAscending)
Sort existing
RealmResults. |
void |
sort(java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2)
Sort existing
RealmResults using two fields. |
void |
sort(java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2,
java.lang.String fieldName3,
boolean sortAscending3)
Sort existing
RealmResults using three fields. |
java.lang.Number |
sum(java.lang.String fieldName)
Calculate the sum of a given field.
|
RealmQuery<E> |
where()
Returns a typed
RealmQuery, which can be used to query for specific
objects of this type. |
addAll, equals, hashCode, lastIndexOf, set, subListaddAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringpublic static final boolean SORT_ORDER_ASCENDING
public static final boolean SORT_ORDER_DESCENDING
public boolean isValid()
RealmResults is still valid to use i.e. the Realm
instance hasn't been closed.true if still valid to use, false otherwise.public RealmQuery<E> where()
RealmQuery, which can be used to query for specific
objects of this type.RealmQuerypublic E get(int location)
get in interface java.util.List<E extends RealmObject>get in class java.util.AbstractList<E extends RealmObject>location - the index of the element to return.java.lang.IndexOutOfBoundsException - if location < 0 || location >= size()public int indexOf(java.lang.Object o)
indexOf in interface java.util.List<E extends RealmObject>indexOf in class java.util.AbstractList<E extends RealmObject>java.lang.NoSuchMethodError - always.public E first()
java.lang.ArrayIndexOutOfBoundsException - if RealmResults is empty.public E last()
java.lang.ArrayIndexOutOfBoundsException - if RealmResults is empty.public java.util.Iterator<E> iterator()
ConcurrentModificationException if accessed.iterator in interface java.lang.Iterable<E extends RealmObject>iterator in interface java.util.Collection<E extends RealmObject>iterator in interface java.util.List<E extends RealmObject>iterator in class java.util.AbstractList<E extends RealmObject>Iteratorpublic java.util.ListIterator<E> listIterator()
ConcurrentModificationException if accessed.listIterator in interface java.util.List<E extends RealmObject>listIterator in class java.util.AbstractList<E extends RealmObject>ListIteratorpublic java.util.ListIterator<E> listIterator(int location)
ConcurrentModificationException if accessed.listIterator in interface java.util.List<E extends RealmObject>listIterator in class java.util.AbstractList<E extends RealmObject>location - the index at which to start the iteration.java.lang.IndexOutOfBoundsException - if location < 0 || location > size()ListIteratorpublic void sort(java.lang.String fieldName)
RealmResults.fieldName - The field name to sort by. Only fields of type boolean, short, int, long,
float, double, Date, and String are supported.java.lang.IllegalArgumentException - if field name does not exist.public void sort(java.lang.String fieldName,
boolean sortAscending)
RealmResults.fieldName - The field name to sort by. Only fields of type boolean, short, int,
long, float, double, Date, and String are supported.sortAscending - The direction to sort by; if true ascending, otherwise descending
You can use the constants SORT_ORDER_ASCENDING and SORT_ORDER_DESCENDING
for readability.java.lang.IllegalArgumentException - if field name does not exist.public void sort(java.lang.String[] fieldNames,
boolean[] sortAscending)
RealmResults.fieldNames - an array of field names to sort by. Only fields of type boolean, short, int,
long, float, double, Date, and String are supported.sortAscending - The directions to sort by; if true ascending, otherwise descending
You can use the constants SORT_ORDER_ASCENDING and SORT_ORDER_DESCENDING
for readability.java.lang.IllegalArgumentException - if a field name does not exist.public void sort(java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2)
RealmResults using two fields.fieldName1 - first field name.sortAscending1 - sort order for first field.fieldName2 - second field name.sortAscending2 - sort order for second field.java.lang.IllegalArgumentException - if a field name does not exist.public void sort(java.lang.String fieldName1,
boolean sortAscending1,
java.lang.String fieldName2,
boolean sortAscending2,
java.lang.String fieldName3,
boolean sortAscending3)
RealmResults using three fields.fieldName1 - first field name.sortAscending1 - sort order for first field.fieldName2 - second field name.sortAscending2 - sort order for second field.fieldName3 - third field name.sortAscending3 - sort order for third field.java.lang.IllegalArgumentException - if a field name does not exist.public int size()
size in interface java.util.Collection<E extends RealmObject>size in interface java.util.List<E extends RealmObject>size in class java.util.AbstractCollection<E extends RealmObject>public java.lang.Number min(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if the field is not a number type.public java.util.Date minDate(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if fieldName is not a Date field.public java.lang.Number max(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if the field is not a number type.public java.util.Date maxDate(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if fieldName is not a Date field.public java.lang.Number sum(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if the field is not a number type.public double average(java.lang.String fieldName)
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.java.lang.IllegalArgumentException - if the field is not a number type.public E remove(int index)
RealmResults.RealmResultsIterator.remove() instead.remove in interface java.util.List<E extends RealmObject>remove in class java.util.AbstractList<E extends RealmObject>index - The array index identifying the object to be removed.null.java.lang.IllegalStateException - if the corresponding Realm is closed or in an incorrect thread.public void removeLast()
removeLast() instead.java.lang.IllegalStateException - if the corresponding Realm is closed or in an incorrect thread.public void clear()
clear in interface java.util.Collection<E extends RealmObject>clear in interface java.util.List<E extends RealmObject>clear in class java.util.AbstractList<E extends RealmObject>java.lang.IllegalStateException - if the corresponding Realm is closed or in an incorrect thread.@Deprecated public boolean add(E element)
add in interface java.util.Collection<E extends RealmObject>add in interface java.util.List<E extends RealmObject>add in class java.util.AbstractList<E extends RealmObject>@Deprecated
public void add(int index,
E element)
add in interface java.util.List<E extends RealmObject>add in class java.util.AbstractList<E extends RealmObject>public boolean isLoaded()
true if the results are not yet loaded, false if they are
still loading. Synchronous query methods like findAll() will always return true,
while asynchronous query methods like findAllAsync() will return false until
the results are available.
This will return true if called for a standalone object (created outside of Realm).true if the query has completed and the data is available false if the
query is still runningpublic boolean load()
true for standalone object (created outside of Realm).
RealmChangeListener when the query completes.true if it successfully completed the query, false otherwise.public void addChangeListener(RealmChangeListener listener)
listener - the change listener to be notifiedpublic void removeChangeListener(RealmChangeListener listener)
listener - the instance to be removed.public void removeChangeListeners()