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 aRealmQueryfor 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
Looperthread, 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
nullnot 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.RealmResultscan contain more elements thanInteger.MAX_VALUE. In that case, you can access only firstInteger.MAX_VALUEelements in it.
-
-
Field Summary
Fields Modifier and Type Field Description io.realm.BaseRealmbaseRealmTheBaseRealminstance in which this collection resides.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(int index, E element)Deprecated.booleanadd(E element)Deprecated.booleanaddAll(int location, Collection<? extends E> collection)Deprecated.booleanaddAll(Collection<? extends E> collection)Deprecated.voidaddChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)Adds a change listener to thisRealmResults.voidaddChangeListener(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.StringasJSON()Returns a JSON representation of the matches of aRealmQuery.doubleaverage(String fieldName)Returns the average of a given field.voidclear()Deprecated.booleancontains(Object object)Searches thisOrderedRealmCollectionfor the specified object.OrderedRealmCollectionSnapshot<E>createSnapshot()Creates a snapshot from thisOrderedRealmCollection.booleandeleteAllFromRealm()This deletes all objects in the collection from the underlying Realm as well as from the collection.booleandeleteFirstFromRealm()Removes the first object in the list.voiddeleteFromRealm(int location)Deletes the object at the given index from the Realm.booleandeleteLastFromRealm()Removes the last object in the list.Efirst()Gets the first object from the collection.Efirst(E defaultValue)Gets the first object from the collection.RealmResults<E>freeze()Eget(int location)Returns the element at the specified location in this list.RealmgetRealm()Returns theRealminstance to which this collection belongs.booleanisFrozen()booleanisLoaded()Returnsfalseif the results are not yet loaded,trueif they are loaded.booleanisManaged()ARealmResultsor aOrderedRealmCollectionSnapshotis always a managed collection.booleanisValid()Checks if the collection is still valid to use, i.e., theRealminstance hasn't been closed.Iterator<E>iterator()Returns an iterator for the results of a query.Elast()Gets the last object from the collection.Elast(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.booleanload()Makes an asynchronous query blocking.Numbermax(String fieldName)Finds the maximum value of a field.DatemaxDate(String fieldName)Finds the maximum date.Numbermin(String fieldName)Finds the minimum value of a field.DateminDate(String fieldName)Finds the minimum date.Eremove(int index)Deprecated.booleanremove(Object object)Deprecated.booleanremoveAll(Collection<?> collection)Deprecated.voidremoveAllChangeListeners()Removes all user-defined change listeners.voidremoveChangeListener(OrderedRealmCollectionChangeListener<RealmResults<E>> listener)Removes the specified change listener.voidremoveChangeListener(RealmChangeListener<RealmResults<E>> listener)Removes the specified change listener.booleanretainAll(Collection<?> collection)Deprecated.Eset(int location, E object)Deprecated.voidsetBlob(String fieldName, byte[] value)Sets the binary value of the given field in all of the objects in the collection.voidsetBoolean(String fieldName, boolean value)Sets thebooleanvalue of the given field in all of the objects in the collection.voidsetByte(String fieldName, byte value)Sets thebytevalue of the given field in all of the objects in the collection.voidsetDate(String fieldName, Date value)Sets theDatevalue of the given field in all of the objects in the collection.voidsetDecimal128(String fieldName, Decimal128 value)Sets theDecimal128value of the given field in all of the objects in the collection.voidsetDouble(String fieldName, double value)Sets thedoublevalue of the given field in all of the objects in the collection.voidsetFloat(String fieldName, float value)Sets thefloatvalue of the given field in all of the objects in the collection.voidsetInt(String fieldName, int value)Sets theintvalue of the given field in all of the objects in the collection.<T> voidsetList(String fieldName, RealmList<T> list)Replaces the RealmList at the given field on all objects in this collection.voidsetLong(String fieldName, long value)Sets thelongvalue of the given field in all of the objects in the collection.voidsetNull(String fieldName)Sets the value tonullfor the given field in all of the objects in the collection.voidsetObject(String fieldName, RealmModel value)Sets a reference to another object on the given field in all of the objects in the collection.voidsetObjectId(String fieldName, ObjectId value)Sets theObjectIdvalue of the given field in all of the objects in the collection.voidsetShort(String fieldName, short value)Sets theshortvalue of the given field in all of the objects in the collection.voidsetString(String fieldName, String value)Sets theStringvalue of the given field in all of the objects in the collection.voidsetUUID(String fieldName, UUID value)Sets theUUIDvalue of the given field in all of the objects in the collection.voidsetValue(String fieldName, Object value)Updates the field given byfieldNamein all objects inside the query result.intsize()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.Numbersum(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:
sortin 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
RealmResultswill be created and returned. The original collection stays unchanged.
-
isLoaded
public boolean isLoaded()
Returnsfalseif the results are not yet loaded,trueif they are loaded.- Returns:
trueif the query has completed and the data is available,falseif the query is still running in the background.
-
load
public boolean load()
Makes an asynchronous query blocking. This will also trigger any registeredRealmChangeListenerwhen the query completes.- Returns:
trueif it successfully completed the query,falseotherwise.
-
setValue
public void setValue(String fieldName, @Nullable Object value)
Updates the field given byfieldNamein all objects inside the query result.This method will automatically try to convert numbers and booleans that are given as
Stringto their appropriate type. For example"10"will be converted to10if 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 tonullfor 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 holdnullvalues.
-
setBoolean
public void setBoolean(String fieldName, boolean value)
Sets thebooleanvalue 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 thebytevalue 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 theshortvalue 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 theintvalue 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 thelongvalue 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 thefloatvalue 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 thedoublevalue 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 theStringvalue 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 theDatevalue 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 aDatefield.
-
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 theDecimal128value 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 aDecimal128field.
-
setObjectId
public void setObjectId(String fieldName, @Nullable ObjectId value)
Sets theObjectIdvalue 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 aObjectIdfield.
-
setUUID
public void setUUID(String fieldName, @Nullable UUID value)
Sets theUUIDvalue 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 aUUIDfield.
-
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 orIntentServicethread.
-
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 orIntentServicethread.
-
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 -onCompletewill 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 callonCompleteorOnError. - 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 theOrderedCollectionChangeSetwill be sent. The changeset will benullthe first time an RealmResults is emitted.RealmResults will continually be emitted as the RealmResults are updated -
onCompletewill 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 callonCompleteorOnError. - 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., theRealminstance hasn't been closed. It will always returntruefor an unmanaged collection.- Specified by:
isValidin interfaceio.realm.internal.ManageableObject- Specified by:
isValidin interfaceRealmCollection<E>- Returns:
trueif it is still valid to use or an unmanaged collection,falseotherwise.
-
isManaged
public boolean isManaged()
ARealmResultsor aOrderedRealmCollectionSnapshotis always a managed collection.- Specified by:
isManagedin interfaceio.realm.internal.ManageableObject- Specified by:
isManagedin interfaceRealmCollection<E>- Returns:
true.- See Also:
RealmCollection.isManaged()
-
contains
public boolean contains(@Nullable Object object)Searches thisOrderedRealmCollectionfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Specified by:
containsin interfaceRealmCollection<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisOrderedRealmCollection,falseotherwise.
-
get
@Nullable public E get(int location)
Returns the element at the specified location in this list.- Specified by:
getin interfaceList<E>- Specified by:
getin 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:
firstin 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:
firstin interfaceOrderedRealmCollection<E>- Returns:
- the first object or the provided default.
-
last
@Nullable public E last()
Gets the last object from the collection.- Specified by:
lastin 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:
lastin 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:
deleteFromRealmin 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:
deleteAllFromRealmin interfaceRealmCollection<E>- Returns:
trueif objects was deleted,falseotherwise.
-
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 aConcurrentModificationExceptionif accessed.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin interfaceList<E>- Overrides:
iteratorin 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 aConcurrentModificationExceptionif accessed.- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin 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 aConcurrentModificationExceptionif accessed.- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin 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:
sortin 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
RealmResultswill 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:
sortin 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
RealmResultswill 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:
sortin 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
RealmResultswill be created and returned. The original collection stays unchanged.
-
size
public int size()
Returns the number of elements in this query result.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein 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:
minin 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
nullas the value for the given field,nullwill be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnullvalues are ignored.
-
minDate
public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDatein 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
nullas the value for the given date field,nullwill be returned. Otherwise the minimum date is returned. When determining the minimum date, objects withnullvalues are ignored.
-
max
public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
maxin 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
nullas the value for the given field,nullwill be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnullvalues are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDatein 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
nullas the value for the given date field,nullwill be returned. Otherwise the maximum date is returned. When determining the maximum date, objects withnullvalues 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:
sumin interfaceRealmCollection<E>- Parameters:
fieldName- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
nullas the value for the given field,0will be returned. When computing the sum, objects withnullvalues are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
averagein 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
nullas the value for the given field,0will be returned. When computing the average, objects withnullvalues are ignored.
-
remove
@Deprecated public E remove(int index)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Throws:
UnsupportedOperationException
-
remove
@Deprecated public boolean remove(Object object)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Throws:
UnsupportedOperationException
-
removeAll
@Deprecated public boolean removeAll(Collection<?> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException
-
set
@Deprecated public E set(int location, E object)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Throws:
UnsupportedOperationException
-
retainAll
@Deprecated public boolean retainAll(Collection<?> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin 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:
deleteLastFromRealmin interfaceOrderedRealmCollection<E>- Returns:
trueif an object was deleted,falseotherwise.- 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:
deleteFirstFromRealmin interfaceOrderedRealmCollection<E>- Returns:
trueif an object was deleted,falseotherwise.- Throws:
IllegalStateException- if the corresponding Realm is closed or in an incorrect thread.
-
clear
@Deprecated public void clear()
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
add
@Deprecated public boolean add(E element)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
add
@Deprecated public void add(int index, E element)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
addAll
@Deprecated public boolean addAll(int location, Collection<? extends E> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractList<E>- Throws:
UnsupportedOperationException- always.
-
addAll
@Deprecated public boolean addAll(Collection<? extends E> collection)
Deprecated.Not supported byRealmResultsandOrderedRealmCollectionSnapshot.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Throws:
UnsupportedOperationException- always.
-
createSnapshot
public OrderedRealmCollectionSnapshot<E> createSnapshot()
Description copied from interface:OrderedRealmCollectionCreates a snapshot from thisOrderedRealmCollection.- Specified by:
createSnapshotin interfaceOrderedRealmCollection<E>- Returns:
- the snapshot of this collection.
- See Also:
OrderedRealmCollectionSnapshot
-
getRealm
public Realm getRealm()
Returns theRealminstance 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:
Realminstance this collection belongs to.- Throws:
IllegalStateException- if the Realm is an instance ofDynamicRealmor theRealmwas already closed.
-
-