E
- The class of objects in this list.public class RealmResults<E> extends 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 OrderedRealmCollectionImpl.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.
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.
|
void |
addChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
Adds a change listener to this
RealmResults . |
void |
addChangeListener(RealmChangeListener<RealmResults<E>> listener)
Adds a change listener to this
RealmResults . |
<any> |
asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmResults.
|
<any> |
asFlowable()
Returns an Rx Flowable that monitors changes to this RealmResults.
|
String |
asJSON()
Returns a JSON representation of the matches of a
RealmQuery . |
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 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.
|
E |
first()
Gets the first object from the collection.
|
E |
first(E defaultValue)
Gets the first object from the collection.
|
RealmResults<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()
Returns
false if the results are not yet loaded, true if they are loaded. |
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()
Makes an asynchronous query blocking.
|
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.
|
void |
removeAllChangeListeners()
Removes all user-defined change listeners.
|
void |
removeChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
Removes the specified change listener.
|
void |
removeChangeListener(RealmChangeListener<RealmResults<E>> listener)
Removes the specified change listener.
|
boolean |
retainAll(Collection<?> collection)
Deprecated.
|
E |
set(int location,
E object)
Deprecated.
|
void |
setBlob(String fieldName,
byte[] value)
Sets the binary value of the given field in all of the objects in the collection.
|
void |
setBoolean(String fieldName,
boolean value)
Sets the
boolean value of the given field in all of the objects in the collection. |
void |
setByte(String fieldName,
byte value)
Sets the
byte value of the given field in all of the objects in the collection. |
void |
setDate(String fieldName,
Date value)
Sets the
Date value of the given field in all of the objects in the collection. |
void |
setDouble(String fieldName,
double value)
Sets the
double value of the given field in all of the objects in the collection. |
void |
setFloat(String fieldName,
float value)
Sets the
float value of the given field in all of the objects in the collection. |
void |
setInt(String fieldName,
int value)
Sets the
int value of the given field in all of the objects in the collection. |
<T> void |
setList(String fieldName,
RealmList<T> list)
Replaces the RealmList at the given field on all objects in this collection.
|
void |
setLong(String fieldName,
long value)
Sets the
long value of the given field in all of the objects in the collection. |
void |
setNull(String fieldName)
Sets the value to
null for the given field in all of the objects in the collection. |
void |
setObject(String fieldName,
RealmModel value)
Sets a reference to another object on the given field in all of the objects in the collection.
|
void |
setShort(String fieldName,
short value)
Sets the
short value of the given field in all of the objects in the collection. |
void |
setString(String fieldName,
String value)
Sets the
String value of the given field in all of the objects in the collection. |
void |
setValue(String fieldName,
Object value)
Updates the field given by
fieldName in all objects inside the query result. |
int |
size()
Returns the number of elements in this query result.
|
RealmResults<E> |
sort(String fieldName)
Sorts a collection based on the provided field in ascending order.
|
RealmResults<E> |
sort(String[] fieldNames,
Sort[] sortOrders)
Sorts a collection based on the provided fields and sort orders.
|
RealmResults<E> |
sort(String fieldName,
Sort sortOrder)
Sorts a collection based on the provided field and sort order.
|
RealmResults<E> |
sort(String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2)
Sorts a collection based on the provided fields and sort orders.
|
Number |
sum(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. |
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 RealmQuery<E> where()
RealmQuery
, which can be used to query for specific objects from this collection.RealmQuery
public RealmResults<E> sort(String fieldName1, Sort sortOrder1, String fieldName2, Sort sortOrder2)
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.public boolean isLoaded()
false
if the results are not yet loaded, true
if they are loaded.true
if the query has completed and the data is available, false
if the query is still
running in the background.public boolean load()
RealmChangeListener
when
the query completes.true
if it successfully completed the query, false
otherwise.public void setValue(String fieldName, Object value)
fieldName
in all objects inside the query result.
This method will automatically try to convert numbers and booleans that are given as
String
to their appropriate type. For example "10"
will be converted to
10
if the field type is RealmFieldType.INTEGER
.
Using the typed setters like setInt(String, int)
will be faster than using
this method.
fieldName
- field to updatevalue
- value to update with.IllegalArgumentException
- if the field could not be found, could not be updated or
the argument didn't match the field type or could not be converted to match the underlying
field type.public void setNull(String fieldName)
null
for the given field in all of the objects in the collection.fieldName
- name of the field to update.IllegalArgumentException
- if field name doesn't exist or is a primary key property.IllegalStateException
- if the field cannot hold null
values.public void setBoolean(String fieldName, boolean value)
boolean
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a boolean field.public void setByte(String fieldName, byte value)
byte
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a byte field.public void setShort(String fieldName, short value)
short
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a short field.public void setInt(String fieldName, int value)
int
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't an integer field.public void setLong(String fieldName, long value)
long
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a long field.public void setFloat(String fieldName, float value)
float
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a float field.public void setDouble(String fieldName, double value)
double
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a double field.public void setString(String fieldName, String value)
String
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a String field.public void setBlob(String fieldName, byte[] value)
fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a binary field.public void setDate(String fieldName, Date value)
Date
value of the given field in all of the objects in the collection.fieldName
- name of the field to update.value
- new value for the field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a date field.public void setObject(String fieldName, RealmModel value)
fieldName
- name of the field to update.value
- new object referenced by this field.IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't an Object reference field.public <T> void setList(String fieldName, RealmList<T> list)
fieldName
- name of the field to update.list
- new value for the field.IllegalArgumentException
- if field name doesn't exist, isn't a RealmList field , if the
objects in the list are not managed or the type of the objects in the list are wrong.public boolean isFrozen()
public RealmResults<E> freeze()
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 addChangeListener(RealmChangeListener<RealmResults<E>> listener)
RealmResults
.
Registering a change listener will not prevent the underlying RealmResults from being garbage collected. If the RealmResults is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.
public class MyActivity extends Activity {
private RealmResults<Person> results; // Strong reference to keep listeners alive
\@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
results = realm.where(Person.class).findAllAsync();
results.addChangeListener(new RealmChangeListener<RealmResults<Person>>() {
\@Override
public void onChange(RealmResults<Person> persons) {
// React to change
}
});
}
}
listener
- the change listener to be notified.IllegalArgumentException
- if the change listener is null
.IllegalStateException
- if you try to add a listener from a non-Looper or
IntentService
thread.public void addChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
RealmResults
.
Registering a change listener will not prevent the underlying RealmResults from being garbage collected. If the RealmResults is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.
public class MyActivity extends Activity {
private RealmResults<Person> results; // Strong reference to keep listeners alive
\@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
results = realm.where(Person.class).findAllAsync();
results.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Person>>() {
\@Override
public void onChange(RealmResults<Person> persons, OrderedCollectionChangeSet changeSet) {
// React to change
}
});
}
}
listener
- the change listener to be notified.IllegalArgumentException
- if the change listener is null
.IllegalStateException
- if you try to add a listener from a non-Looper or
IntentService
thread.public void removeAllChangeListeners()
IllegalStateException
- if you try to remove listeners from a non-Looper Thread.RealmChangeListener
public void removeChangeListener(RealmChangeListener<RealmResults<E>> listener)
listener
- the change listener to be removed.IllegalArgumentException
- if the change listener is null
.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.RealmChangeListener
public void removeChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
listener
- the change listener to be removed.IllegalArgumentException
- if the change listener is null
.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.RealmChangeListener
public <any> asFlowable()
onComplete
will never be called.
Items emitted from Realm Flowables are frozen (See freeze()
. This means that they
are immutable and can be read on any thread.
Realm Flowables always emit items from the thread holding the live RealmResults. This means that if you need to do further processing, it is recommend to observe the values on a computation scheduler:
realm.where(Foo.class).findAllAsync().asFlowable()
.observeOn(Schedulers.computation())
.map(rxResults -> doExpensiveWork(rxResults))
.observeOn(AndroidSchedulers.mainThread())
.subscribe( ... );
If you would like the asFlowable()
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().asFlowable()
.filter(results -> results.isLoaded())
.first()
.subscribe( ... ) // You only get the results once
onNext
. It will never call onComplete
or OnError
.UnsupportedOperationException
- if the required RxJava framework is not on the classpath or the
corresponding Realm instance doesn't support RxJava.IllegalStateException
- if the Realm wasn't opened on a Looper thread.public <any> asChangesetObservable()
OrderedCollectionChangeSet
will be sent. The changeset will be null
the first
time an RealmResults is emitted.
RealmResults will continually be emitted as the RealmResults are updated - onComplete
will never be called.
Items emitted from Realm Observables are frozen (See freeze()
. This means that they
are immutable and can be read on any thread.
Realm Observables always emit items from the thread holding the live Realm. This means that if you need to do further processing, it is recommend to observe the values on a computation scheduler:
realm.where(Foo.class).findAllAsync().asChangesetObservable()
.observeOn(Schedulers.computation())
.map((rxResults, changes) -> doExpensiveWork(rxResults, changes))
.observeOn(AndroidSchedulers.mainThread())
.subscribe( ... );
onNext
. It will never call onComplete
or OnError
.UnsupportedOperationException
- if the required RxJava framework is not on the classpath or the
corresponding Realm instance doesn't support RxJava.IllegalStateException
- if the Realm wasn't opened on a Looper thread.public String asJSON()
RealmQuery
. Cycles will be returned as row indices.
This is a helper method used to inspect data, or for debugging purpose, this method could pull a large string which
could cause an OutOfMemory error.RealmQuery
.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 void deleteFromRealm(int location)
deleteFromRealm
in interface OrderedRealmCollection<E>
location
- the array index identifying the object to be removed.public boolean deleteAllFromRealm()
deleteAllFromRealm
in interface RealmCollection<E>
true
if objects was deleted, false
otherwise.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 RealmResults<E> sort(String fieldName)
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.public RealmResults<E> sort(String fieldName, Sort sortOrder)
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.public RealmResults<E> sort(String[] fieldNames, Sort[] sortOrders)
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.public int size()
size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
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
public boolean deleteLastFromRealm()
deleteLastFromRealm
in interface OrderedRealmCollection<E>
true
if an object was deleted, false
otherwise.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.public boolean deleteFirstFromRealm()
deleteFirstFromRealm
in interface OrderedRealmCollection<E>
true
if an object was deleted, false
otherwise.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.@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 OrderedRealmCollectionSnapshot<E> createSnapshot()
OrderedRealmCollection
OrderedRealmCollection
.createSnapshot
in interface OrderedRealmCollection<E>
OrderedRealmCollectionSnapshot
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.