Class RealmResults<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- io.realm.RealmResults<E>
-
- Type Parameters:
E
- The class of objects in this list.
- All Implemented Interfaces:
io.realm.internal.Freezable<RealmCollection<E>>
,io.realm.internal.ManageableObject
,OrderedRealmCollection<E>
,RealmCollection<E>
,Iterable<E>
,Collection<E>
,List<E>
public class RealmResults<E> extends AbstractList<E>
This class holds all the matches of aRealmQuery
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 theList.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 thanInteger.MAX_VALUE
. In that case, you can access only firstInteger.MAX_VALUE
elements in it.
-
-
Field Summary
Fields Modifier and Type Field Description io.realm.BaseRealm
baseRealm
TheBaseRealm
instance in which this collection resides.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
add(int index, E element)
Deprecated.boolean
add(E element)
Deprecated.boolean
addAll(int location, Collection<? extends E> collection)
Deprecated.boolean
addAll(Collection<? extends E> collection)
Deprecated.void
addChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
Adds a change listener to thisRealmResults
.void
addChangeListener(RealmChangeListener<RealmResults<E>> listener)
Adds a change listener to thisRealmResults
.Observable<CollectionChange<RealmResults<E>>>
asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmResults.Flowable<RealmResults<E>>
asFlowable()
Returns an Rx Flowable that monitors changes to this RealmResults.String
asJSON()
Returns a JSON representation of the matches of aRealmQuery
.double
average(String fieldName)
Returns the average of a given field.void
clear()
Deprecated.boolean
contains(Object object)
Searches thisOrderedRealmCollection
for the specified object.OrderedRealmCollectionSnapshot<E>
createSnapshot()
Creates a snapshot from thisOrderedRealmCollection
.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()
E
get(int location)
Returns the element at the specified location in this list.Realm
getRealm()
Returns theRealm
instance to which this collection belongs.boolean
isFrozen()
boolean
isLoaded()
Returnsfalse
if the results are not yet loaded,true
if they are loaded.boolean
isManaged()
ARealmResults
or aOrderedRealmCollectionSnapshot
is always a managed collection.boolean
isValid()
Checks if the collection is still valid to use, i.e., theRealm
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 theboolean
value of the given field in all of the objects in the collection.void
setByte(String fieldName, byte value)
Sets thebyte
value of the given field in all of the objects in the collection.void
setDate(String fieldName, Date value)
Sets theDate
value of the given field in all of the objects in the collection.void
setDecimal128(String fieldName, Decimal128 value)
Sets theDecimal128
value of the given field in all of the objects in the collection.void
setDouble(String fieldName, double value)
Sets thedouble
value of the given field in all of the objects in the collection.void
setFloat(String fieldName, float value)
Sets thefloat
value of the given field in all of the objects in the collection.void
setInt(String fieldName, int value)
Sets theint
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 thelong
value of the given field in all of the objects in the collection.void
setNull(String fieldName)
Sets the value tonull
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
setObjectId(String fieldName, ObjectId value)
Sets theObjectId
value of the given field in all of the objects in the collection.void
setShort(String fieldName, short value)
Sets theshort
value of the given field in all of the objects in the collection.void
setString(String fieldName, String value)
Sets theString
value of the given field in all of the objects in the collection.void
setUUID(String fieldName, UUID value)
Sets theUUID
value of the given field in all of the objects in the collection.void
setValue(String fieldName, Object value)
Updates the field given byfieldName
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 aRealmQuery
, which can be used to query for specific objects from this collection.-
Methods inherited from class java.util.AbstractList
equals, hashCode, indexOf, lastIndexOf, subList
-
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, toArray, toArray, toString
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
containsAll, equals, hashCode, indexOf, isEmpty, lastIndexOf, replaceAll, sort, spliterator, subList, toArray, toArray
-
-
-
-
Method Detail
-
where
public RealmQuery<E> where()
Returns aRealmQuery
, which can be used to query for specific objects from this collection.- Returns:
- a RealmQuery object.
- See Also:
RealmQuery
-
sort
public RealmResults<E> sort(String fieldName1, Sort sortOrder1, String fieldName2, Sort sortOrder2)
Sorts a collection based on the provided fields and sort orders.- Specified by:
sort
in interfaceOrderedRealmCollection<E>
- Parameters:
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.- Returns:
- a new sorted
RealmResults
will be created and returned. The original collection stays unchanged.
-
isLoaded
public boolean isLoaded()
Returnsfalse
if the results are not yet loaded,true
if they are loaded.- Returns:
true
if the query has completed and the data is available,false
if the query is still running in the background.
-
load
public boolean load()
Makes an asynchronous query blocking. This will also trigger any registeredRealmChangeListener
when the query completes.- Returns:
true
if it successfully completed the query,false
otherwise.
-
setValue
public void setValue(String fieldName, @Nullable Object value)
Updates the field given byfieldName
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 to10
if the field type isRealmFieldType.INTEGER
.Using the typed setters like
setInt(String, int)
will be faster than using this method.- Parameters:
fieldName
- field to updatevalue
- value to update with.- Throws:
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.
-
setNull
public void setNull(String fieldName)
Sets the value tonull
for the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.- Throws:
IllegalArgumentException
- if field name doesn't exist or is a primary key property.IllegalStateException
- if the field cannot holdnull
values.
-
setBoolean
public void setBoolean(String fieldName, boolean value)
Sets theboolean
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a boolean field.
-
setByte
public void setByte(String fieldName, byte value)
Sets thebyte
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a byte field.
-
setShort
public void setShort(String fieldName, short value)
Sets theshort
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a short field.
-
setInt
public void setInt(String fieldName, int value)
Sets theint
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't an integer field.
-
setLong
public void setLong(String fieldName, long value)
Sets thelong
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a long field.
-
setFloat
public void setFloat(String fieldName, float value)
Sets thefloat
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a float field.
-
setDouble
public void setDouble(String fieldName, double value)
Sets thedouble
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a double field.
-
setString
public void setString(String fieldName, @Nullable String value)
Sets theString
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a String field.
-
setBlob
public void setBlob(String fieldName, @Nullable byte[] value)
Sets the binary value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't a binary field.
-
setDate
public void setDate(String fieldName, @Nullable Date value)
Sets theDate
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't aDate
field.
-
setObject
public void setObject(String fieldName, @Nullable RealmModel value)
Sets a reference to another object on the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new object referenced by this field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't an Object reference field.
-
setDecimal128
public void setDecimal128(String fieldName, @Nullable Decimal128 value)
Sets theDecimal128
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't aDecimal128
field.
-
setObjectId
public void setObjectId(String fieldName, @Nullable ObjectId value)
Sets theObjectId
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't aObjectId
field.
-
setUUID
public void setUUID(String fieldName, @Nullable UUID value)
Sets theUUID
value of the given field in all of the objects in the collection.- Parameters:
fieldName
- name of the field to update.value
- new value for the field.- Throws:
IllegalArgumentException
- if field name doesn't exist, is a primary key property or isn't aUUID
field.
-
setList
public <T> void setList(String fieldName, RealmList<T> list)
Replaces the RealmList at the given field on all objects in this collection.- Parameters:
fieldName
- name of the field to update.list
- new value for the field.- Throws:
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.
-
isFrozen
public boolean isFrozen()
-
freeze
public RealmResults<E> freeze()
-
addChangeListener
public void addChangeListener(RealmChangeListener<RealmResults<E>> listener)
Adds a change listener to thisRealmResults
.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 } }); } }
- Parameters:
listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if the change listener isnull
.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.
-
addChangeListener
public void addChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
Adds a change listener to thisRealmResults
.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 } }); } }
- Parameters:
listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if the change listener isnull
.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.
-
removeAllChangeListeners
public void removeAllChangeListeners()
Removes all user-defined change listeners.- Throws:
IllegalStateException
- if you try to remove listeners from a non-Looper Thread.- See Also:
RealmChangeListener
-
removeChangeListener
public void removeChangeListener(RealmChangeListener<RealmResults<E>> listener)
Removes the specified change listener.- Parameters:
listener
- the change listener to be removed.- Throws:
IllegalArgumentException
- if the change listener isnull
.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.- See Also:
RealmChangeListener
-
removeChangeListener
public void removeChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)
Removes the specified change listener.- Parameters:
listener
- the change listener to be removed.- Throws:
IllegalArgumentException
- if the change listener isnull
.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.- See Also:
RealmChangeListener
-
asFlowable
public Flowable<RealmResults<E>> asFlowable()
Returns an Rx Flowable that monitors changes to this RealmResults. It will emit the current RealmResults when subscribed to. RealmResults will continually be emitted as the RealmResults are updated -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 thefirst()
operator:realm.where(Foo.class).findAllAsync().asFlowable() .filter(results -> results.isLoaded()) .first() .subscribe( ... ) // You only get the results once
- Returns:
- RxJava Observable that only calls
onNext
. It will never callonComplete
orOnError
. - Throws:
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.- See Also:
- RxJava and Realm
-
asChangesetObservable
public Observable<CollectionChange<RealmResults<E>>> asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmResults. It will emit the current RealmResults when subscribed. For each update to the RealmResult a pair consisting of the RealmResults and theOrderedCollectionChangeSet
will be sent. The changeset will benull
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( ... );
- Returns:
- RxJava Observable that only calls
onNext
. It will never callonComplete
orOnError
. - Throws:
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.- See Also:
- RxJava and Realm
-
asJSON
public String asJSON()
Returns a JSON representation of the matches of aRealmQuery
. 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.- Returns:
- string representation of a JSON array containing entries of the resulting
RealmQuery
.
-
isValid
public boolean isValid()
Checks if the collection is still valid to use, i.e., theRealm
instance hasn't been closed. It will always returntrue
for an unmanaged collection.- Specified by:
isValid
in interfaceio.realm.internal.ManageableObject
- Specified by:
isValid
in interfaceRealmCollection<E>
- Returns:
true
if it is still valid to use or an unmanaged collection,false
otherwise.
-
isManaged
public boolean isManaged()
ARealmResults
or aOrderedRealmCollectionSnapshot
is always a managed collection.- Specified by:
isManaged
in interfaceio.realm.internal.ManageableObject
- Specified by:
isManaged
in interfaceRealmCollection<E>
- Returns:
true
.- See Also:
RealmCollection.isManaged()
-
contains
public boolean contains(@Nullable Object object)
Searches thisOrderedRealmCollection
for the specified object.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Specified by:
contains
in interfaceRealmCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
object
- the object to search for.- Returns:
true
ifobject
is an element of thisOrderedRealmCollection
,false
otherwise.
-
get
@Nullable public E get(int location)
Returns the element at the specified location in this list.- Specified by:
get
in interfaceList<E>
- Specified by:
get
in classAbstractList<E>
- Parameters:
location
- the index of the element to return.- Returns:
- the element at the specified index.
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location >= size()
.
-
first
@Nullable public E first()
Gets the first object from the collection.- Specified by:
first
in interfaceOrderedRealmCollection<E>
- Returns:
- the first object.
-
first
@Nullable public E first(@Nullable E defaultValue)
Gets the first object from the collection. If the collection is empty, the provided default will be used instead.- Specified by:
first
in interfaceOrderedRealmCollection<E>
- Returns:
- the first object or the provided default.
-
last
@Nullable public E last()
Gets the last object from the collection.- Specified by:
last
in interfaceOrderedRealmCollection<E>
- Returns:
- the last object.
-
last
@Nullable public E last(@Nullable E defaultValue)
Gets the last object from the collection. If the collection is empty, the provided default will be used instead.- Specified by:
last
in interfaceOrderedRealmCollection<E>
- Returns:
- the last object or the provided default.
-
deleteFromRealm
public void deleteFromRealm(int location)
Deletes the object at the given index from the Realm. This also removes it from the collection.- Specified by:
deleteFromRealm
in interfaceOrderedRealmCollection<E>
- Parameters:
location
- the array index identifying the object to be removed.
-
deleteAllFromRealm
public boolean deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.- Specified by:
deleteAllFromRealm
in interfaceRealmCollection<E>
- Returns:
true
if objects was deleted,false
otherwise.
-
iterator
public Iterator<E> iterator()
Returns an iterator for the results of a query. Any change to Realm while iterating will cause this iterator to throw aConcurrentModificationException
if accessed.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceList<E>
- Overrides:
iterator
in classAbstractList<E>
- Returns:
- an iterator on the elements of this list.
- See Also:
Iterator
-
listIterator
public ListIterator<E> listIterator()
Returns a list iterator for the results of a query. Any change to Realm while iterating will cause the iterator to throw aConcurrentModificationException
if accessed.- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
- Returns:
- a ListIterator on the elements of this list.
- See Also:
ListIterator
-
listIterator
public ListIterator<E> listIterator(int location)
Returns a list iterator on the results of a query. Any change to Realm while iterating will cause the iterator to throw aConcurrentModificationException
if accessed.- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
- Parameters:
location
- the index at which to start the iteration.- Returns:
- a ListIterator on the elements of this list.
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location > size()
.- See Also:
ListIterator
-
sort
public RealmResults<E> sort(String fieldName)
Sorts a collection based on the provided field in ascending order.- Specified by:
sort
in interfaceOrderedRealmCollection<E>
- Parameters:
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date, and String are supported.- Returns:
- a new sorted
RealmResults
will be created and returned. The original collection stays unchanged.
-
sort
public RealmResults<E> sort(String fieldName, Sort sortOrder)
Sorts a collection based on the provided field and sort order.- Specified by:
sort
in interfaceOrderedRealmCollection<E>
- Parameters:
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.- Returns:
- a new sorted
RealmResults
will be created and returned. The original collection stays unchanged.
-
sort
public RealmResults<E> sort(String[] fieldNames, Sort[] sortOrders)
Sorts a collection based on the provided fields and sort orders.- Specified by:
sort
in interfaceOrderedRealmCollection<E>
- Parameters:
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.- Returns:
- a new sorted
RealmResults
will be created and returned. The original collection stays unchanged.
-
size
public int size()
Returns the number of elements in this query result.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceList<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this query result.
-
min
public Number min(String fieldName)
Finds the minimum value of a field.- Specified by:
min
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to look for a minimum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
null
as the value for the given field,null
will be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnull
values are ignored.
-
minDate
public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDate
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to look for the minimum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
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 withnull
values are ignored.
-
max
public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
max
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to look for a maximum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
null
as the value for the given field,null
will be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnull
values are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDate
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to look for the maximum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
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 withnull
values are ignored. - Throws:
IllegalArgumentException
- if fieldName is not a Date field.
-
sum
public Number sum(String fieldName)
Calculates the sum of a given field.- Specified by:
sum
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
null
as the value for the given field,0
will be returned. When computing the sum, objects withnull
values are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
average
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to calculate average on. Only number fields are supported.- Returns:
- the average for the given field amongst objects in query results. This will be of type double for all
types of number fields. If no objects exist or they all have
null
as the value for the given field,0
will be returned. When computing the average, objects withnull
values are ignored.
-
remove
@Deprecated public E remove(int index)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractList<E>
- Throws:
UnsupportedOperationException
-
remove
@Deprecated public boolean remove(Object object)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
removeAll
@Deprecated public boolean removeAll(Collection<?> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
set
@Deprecated public E set(int location, E object)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
set
in interfaceList<E>
- Overrides:
set
in classAbstractList<E>
- Throws:
UnsupportedOperationException
-
retainAll
@Deprecated public boolean retainAll(Collection<?> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Overrides:
retainAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
-
deleteLastFromRealm
public boolean deleteLastFromRealm()
Removes the last object in the list. This also deletes the object from the underlying Realm.- Specified by:
deleteLastFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,false
otherwise.- Throws:
IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.
-
deleteFirstFromRealm
public boolean deleteFirstFromRealm()
Removes the first object in the list. This also deletes the object from the underlying Realm.- Specified by:
deleteFirstFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,false
otherwise.- Throws:
IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.
-
clear
@Deprecated public void clear()
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
add
@Deprecated public boolean add(E element)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
add
@Deprecated public void add(int index, E element)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
addAll
@Deprecated public boolean addAll(int location, Collection<? extends E> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractList<E>
- Throws:
UnsupportedOperationException
- always.
-
addAll
@Deprecated public boolean addAll(Collection<? extends E> collection)
Deprecated.Not supported byRealmResults
andOrderedRealmCollectionSnapshot
.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Throws:
UnsupportedOperationException
- always.
-
createSnapshot
public OrderedRealmCollectionSnapshot<E> createSnapshot()
Description copied from interface:OrderedRealmCollection
Creates a snapshot from thisOrderedRealmCollection
.- Specified by:
createSnapshot
in interfaceOrderedRealmCollection<E>
- Returns:
- the snapshot of this collection.
- See Also:
OrderedRealmCollectionSnapshot
-
getRealm
public Realm getRealm()
Returns theRealm
instance to which this collection belongs.Calling
Closeable.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.- Returns:
Realm
instance this collection belongs to.- Throws:
IllegalStateException
- if the Realm is an instance ofDynamicRealm
or theRealm
was already closed.
-
-