E
- The class of objects in this list.public 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 | Method and Description |
---|---|
boolean |
add(E element)
Deprecated.
|
void |
add(int index,
E element)
Deprecated.
|
void |
addChangeListener(RealmChangeListener listener)
Adds a change listener to this RealmResults.
|
<any> |
asObservable()
Returns an Rx Observable that monitors changes 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()
Gets 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()
Checks 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()
Gets 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()
Makes an asynchronous query blocking.
|
java.lang.Number |
max(java.lang.String fieldName)
Finds the maximum value of a field.
|
java.util.Date |
maxDate(java.lang.String fieldName)
Finds the maximum date.
|
java.lang.Number |
min(java.lang.String fieldName)
Finds the minimum value of a field.
|
java.util.Date |
minDate(java.lang.String fieldName)
Finds the minimum date.
|
E |
remove(int index)
Removes an object at a given index.
|
void |
removeChangeListener(RealmChangeListener listener)
Removes a previously registered listener.
|
void |
removeChangeListeners()
Removes 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)
Sorts (ascending) an existing
RealmResults . |
void |
sort(java.lang.String[] fieldNames,
Sort[] sortOrders)
Sorts existing
RealmResults . |
void |
sort(java.lang.String fieldName,
Sort sortOrder)
Sorts existing
RealmResults . |
void |
sort(java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Sorts existing
RealmResults using two fields. |
void |
sort(java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2,
java.lang.String fieldName3,
Sort sortOrder3)
Sorts existing
RealmResults using three fields. |
java.lang.Number |
sum(java.lang.String fieldName)
Calculates 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, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
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.RealmQuery
public 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>
Iterator
public 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>
ListIterator
public 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()
.ListIterator
public 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, Sort sortOrder)
RealmResults
.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.java.lang.IllegalArgumentException
- if field name does not exist.public void sort(java.lang.String[] fieldNames, Sort[] sortOrders)
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.sortOrders
- the directions to sort by.java.lang.IllegalArgumentException
- if a field name does not exist.public void sort(java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
RealmResults
using two fields.fieldName1
- first field name.sortOrder1
- sort order for first field.fieldName2
- second field name.sortOrder2
- sort order for second field.java.lang.IllegalArgumentException
- if a field name does not exist.public void sort(java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2, java.lang.String fieldName3, Sort sortOrder3)
RealmResults
using three fields.fieldName1
- first field name.sortOrder1
- sort order for first field.fieldName2
- second field name.sortOrder2
- sort order for second field.fieldName3
- third field name.sortOrder3
- 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
running.public 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 notified.public void removeChangeListener(RealmChangeListener listener)
listener
- the instance to be removed.public void removeChangeListeners()
public <any> asObservable()
java.lang.UnsupportedOperationException
- if the required RxJava framework is not on the classpath.