E
- The class of objects in this list.public final class RealmResults<E extends RealmModel> extends java.util.AbstractList<E> implements OrderedRealmCollection<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.
If a RealmResults is built on RealmList through RealmList.where()
, it will become empty when the source
RealmList gets deleted.
RealmResults
can contain more elements than Integer.MAX_VALUE
.
In that case, you can access only first Integer.MAX_VALUE
elements in it.
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.
|
boolean |
addAll(java.util.Collection<? extends E> collection)
Deprecated.
|
boolean |
addAll(int location,
java.util.Collection<? extends E> collection)
Deprecated.
|
void |
addChangeListener(RealmChangeListener<RealmResults<E>> 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()
Deprecated.
|
boolean |
contains(java.lang.Object object)
Searches this
RealmResults for the specified object. |
boolean |
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.
|
boolean |
deleteFirstFromRealm()
Removes the first object in the list.
|
void |
deleteFromRealm(int location)
Deletes the object at the given index from the Realm.
|
boolean |
deleteLastFromRealm()
Removes the last object in the list.
|
RealmResults<E> |
distinct(java.lang.String fieldName)
Returns a distinct set of objects of a specific class.
|
RealmResults<E> |
distinct(java.lang.String firstFieldName,
java.lang.String... remainingFieldNames)
Returns a distinct set of objects from a specific class.
|
RealmResults<E> |
distinctAsync(java.lang.String fieldName)
Asynchronously returns a distinct set of objects of a specific class.
|
E |
first()
Gets the first object from the collection.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
boolean |
isLoaded()
Returns
true if the results are not yet loaded, false if they are still loading. |
boolean |
isValid()
Checks if the collection is still valid to use e.g.
|
java.util.Iterator<E> |
iterator()
Returns an iterator for the results of a query.
|
E |
last()
Gets the last object from the collection.
|
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)
Deprecated.
|
boolean |
remove(java.lang.Object object)
Deprecated.
|
boolean |
removeAll(java.util.Collection<?> collection)
Deprecated.
|
void |
removeChangeListener(RealmChangeListener listener)
Removes a previously registered listener.
|
void |
removeChangeListeners()
Removes all registered listeners.
|
void |
removeLast()
Deprecated.
|
boolean |
retainAll(java.util.Collection<?> collection)
Deprecated.
|
E |
set(int location,
E object)
Deprecated.
|
int |
size()
Returns the number of elements in this query result.
|
RealmResults<E> |
sort(java.lang.String fieldName)
Sorts a collection based on the provided field in ascending order.
|
RealmResults<E> |
sort(java.lang.String[] fieldNames,
Sort[] sortOrders)
Sorts a collection based on the provided fields and sort orders.
|
RealmResults<E> |
sort(java.lang.String fieldName,
Sort sortOrder)
Sorts a collection based on the provided field and sort order.
|
RealmResults<E> |
sort(java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Sorts a collection based on the provided fields and sort orders.
|
void |
sort(java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2,
java.lang.String fieldName3,
Sort sortOrder3)
Deprecated.
|
java.lang.Number |
sum(java.lang.String fieldName)
Calculates the sum of a given field.
|
RealmQuery<E> |
where()
Returns a
RealmQuery , which can be used to query for specific objects from this collection. |
containsAll, isEmpty, toArray, toArray, toString
public boolean isValid()
Realm
instance hasn't
been closed.isValid
in interface RealmCollection<E extends RealmModel>
true
if still valid to use, false
otherwise.public RealmQuery<E> where()
RealmQuery
, which can be used to query for specific objects from this collection.where
in interface RealmCollection<E extends RealmModel>
RealmQuery
public boolean contains(java.lang.Object object)
RealmResults
for the specified object.contains
in interface RealmCollection<E extends RealmModel>
contains
in interface java.util.Collection<E extends RealmModel>
contains
in interface java.util.List<E extends RealmModel>
contains
in class java.util.AbstractCollection<E extends RealmModel>
object
- the object to search for.true
if object
is an element of this RealmResults
,
false
otherwisepublic E get(int location)
get
in interface java.util.List<E extends RealmModel>
get
in class java.util.AbstractList<E extends RealmModel>
location
- the index of the element to return.java.lang.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public E first()
first
in interface OrderedRealmCollection<E extends RealmModel>
public E last()
last
in interface OrderedRealmCollection<E extends RealmModel>
public void deleteFromRealm(int location)
deleteFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
location
- the array index identifying the object to be removed.public boolean deleteAllFromRealm()
deleteAllFromRealm
in interface RealmCollection<E extends RealmModel>
true
if objects was deleted, false
otherwise.public java.util.Iterator<E> iterator()
ConcurrentModificationException
if accessed.iterator
in interface java.lang.Iterable<E extends RealmModel>
iterator
in interface java.util.Collection<E extends RealmModel>
iterator
in interface java.util.List<E extends RealmModel>
iterator
in class java.util.AbstractList<E extends RealmModel>
Iterator
public java.util.ListIterator<E> listIterator()
ConcurrentModificationException
if accessed.listIterator
in interface java.util.List<E extends RealmModel>
listIterator
in class java.util.AbstractList<E extends RealmModel>
ListIterator
public java.util.ListIterator<E> listIterator(int location)
ConcurrentModificationException
if accessed.listIterator
in interface java.util.List<E extends RealmModel>
listIterator
in class java.util.AbstractList<E extends RealmModel>
location
- the index at which to start the iteration.java.lang.IndexOutOfBoundsException
- if location < 0 || location > size()
.ListIterator
public RealmResults<E> sort(java.lang.String fieldName)
sort
in interface OrderedRealmCollection<E extends RealmModel>
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date,
and String are supported.public RealmResults<E> sort(java.lang.String fieldName, Sort sortOrder)
sort
in interface OrderedRealmCollection<E extends RealmModel>
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.public RealmResults<E> sort(java.lang.String[] fieldNames, Sort[] sortOrders)
sort
in interface OrderedRealmCollection<E extends RealmModel>
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.public RealmResults<E> sort(java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
sort
in interface OrderedRealmCollection<E extends RealmModel>
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.@Deprecated 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.
DEPRECATED: Use sort(String[], Sort[])
instead.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 RealmModel>
size
in interface java.util.List<E extends RealmModel>
size
in class java.util.AbstractCollection<E extends RealmModel>
public java.lang.Number min(java.lang.String fieldName)
min
in interface RealmCollection<E extends RealmModel>
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 java.util.Date minDate(java.lang.String fieldName)
minDate
in interface RealmCollection<E extends RealmModel>
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 java.lang.Number max(java.lang.String fieldName)
max
in interface RealmCollection<E extends RealmModel>
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 java.util.Date maxDate(java.lang.String fieldName)
maxDate
in interface RealmCollection<E extends RealmModel>
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)
sum
in interface RealmCollection<E extends RealmModel>
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(java.lang.String fieldName)
average
in interface RealmCollection<E extends RealmModel>
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.public RealmResults<E> distinct(java.lang.String fieldName)
fieldName
- the field name.RealmResults
containing the distinct objects.java.lang.IllegalArgumentException
- if a field is null, does not exist, is an unsupported type,
is not indexed, or points to linked fields.public RealmResults<E> distinctAsync(java.lang.String fieldName)
fieldName
- the field name.RealmResults
. Users need to register a listener
addChangeListener(RealmChangeListener)
to be notified when the
query completes.java.lang.IllegalArgumentException
- if a field is null, does not exist, is an unsupported type,
is not indexed, or points to linked fields.public RealmResults<E> distinct(java.lang.String firstFieldName, java.lang.String... remainingFieldNames)
firstFieldName
- first field name to use when finding distinct objects.remainingFieldNames
- remaining field names when determining all unique combinations of field values.RealmResults
containing the distinct objects.java.lang.IllegalArgumentException
- if field names is empty or null
, does not exist,
is an unsupported type, or points to a linked field.@Deprecated public E remove(int index)
remove
in interface java.util.List<E extends RealmModel>
remove
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
@Deprecated public boolean remove(java.lang.Object object)
remove
in interface java.util.Collection<E extends RealmModel>
remove
in interface java.util.List<E extends RealmModel>
remove
in class java.util.AbstractCollection<E extends RealmModel>
java.lang.UnsupportedOperationException
@Deprecated public boolean removeAll(java.util.Collection<?> collection)
removeAll
in interface java.util.Collection<E extends RealmModel>
removeAll
in interface java.util.List<E extends RealmModel>
removeAll
in class java.util.AbstractCollection<E extends RealmModel>
java.lang.UnsupportedOperationException
@Deprecated public E set(int location, E object)
set
in interface java.util.List<E extends RealmModel>
set
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
@Deprecated public boolean retainAll(java.util.Collection<?> collection)
retainAll
in interface java.util.Collection<E extends RealmModel>
retainAll
in interface java.util.List<E extends RealmModel>
retainAll
in class java.util.AbstractCollection<E extends RealmModel>
java.lang.UnsupportedOperationException
@Deprecated public void removeLast()
deleteLastFromRealm()
instead.java.lang.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.public boolean deleteLastFromRealm()
deleteLastFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
true
if an object was deleted, false
otherwise.java.lang.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.public boolean deleteFirstFromRealm()
deleteFirstFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
true
if an object was deleted, false
otherwise.java.lang.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.@Deprecated public void clear()
clear
in interface java.util.Collection<E extends RealmModel>
clear
in interface java.util.List<E extends RealmModel>
clear
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
- always.@Deprecated public boolean add(E element)
add
in interface java.util.Collection<E extends RealmModel>
add
in interface java.util.List<E extends RealmModel>
add
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
- always.@Deprecated public void add(int index, E element)
add
in interface java.util.List<E extends RealmModel>
add
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
- always.@Deprecated public boolean addAll(int location, java.util.Collection<? extends E> collection)
addAll
in interface java.util.List<E extends RealmModel>
addAll
in class java.util.AbstractList<E extends RealmModel>
java.lang.UnsupportedOperationException
- always.@Deprecated public boolean addAll(java.util.Collection<? extends E> collection)
addAll
in interface java.util.Collection<E extends RealmModel>
addAll
in interface java.util.List<E extends RealmModel>
addAll
in class java.util.AbstractCollection<E extends RealmModel>
java.lang.UnsupportedOperationException
- always.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).isLoaded
in interface RealmCollection<E extends RealmModel>
true
if the query has completed and the data is available false
if the query is still
running.public boolean load()
RealmChangeListener
when
the query completes.load
in interface RealmCollection<E extends RealmModel>
true
if it successfully completed the query, false
otherwise. true
will always
be returned for standalone objects.public void addChangeListener(RealmChangeListener<RealmResults<E>> 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()
onComplete
will never be called.
If you would like the asObservable()
to stop emitting items you can instruct RxJava to
only emit only the first item by using the first()
operator:
realm.where(Foo.class).findAllAsync().asObservable()
.filter(results -> results.isLoaded())
.first()
.subscribe( ... ) // You only get the results once
onNext
. It will never call onComplete
or OnError
.java.lang.UnsupportedOperationException
- if the required RxJava framework is not on the classpath or the
corresponding Realm instance doesn't support RxJava.