Class RealmList<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- io.realm.RealmList<E>
-
- Type Parameters:
E
- the class of objects in 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 RealmList<E> extends AbstractList<E> implements OrderedRealmCollection<E>
RealmList is used to model one-to-many relationships in aRealmObject
. RealmList has two modes: A managed and unmanaged mode. In managed mode all objects are persisted inside a Realm, in unmanaged mode it works as a normal ArrayList.Only Realm can create managed RealmLists. Managed RealmLists will automatically update the content whenever the underlying Realm is updated, and can only be accessed using the getter of a
RealmObject
.Unmanaged RealmLists can be created by the user and can contain both managed and unmanaged RealmObjects. This is useful when dealing with JSON deserializers like GSON or other frameworks that inject values into a class. Unmanaged elements in this list can be added to a Realm using the
Realm.copyToRealm(Iterable, ImportFlag...)
method.RealmList
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 list resides.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int location, E element)
Inserts the specified object into this List at the specified location.boolean
add(E object)
Adds the specified object at the end of this List.void
addChangeListener(OrderedRealmCollectionChangeListener<RealmList<E>> listener)
Adds a change listener to thisRealmList
.void
addChangeListener(RealmChangeListener<RealmList<E>> listener)
Adds a change listener to thisRealmList
.Observable<CollectionChange<RealmList<E>>>
asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmList.Flowable<RealmList<E>>
asFlowable()
Returns an Rx Flowable that monitors changes to this RealmList.double
average(String fieldName)
Returns the average of a given field.void
clear()
Removes all elements from this list, leaving it empty.boolean
contains(Object object)
Returnstrue
if the list contains the specified element when attached to a Realm.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()
Deletes the first object from the Realm.void
deleteFromRealm(int location)
Deletes the object at the given index from the Realm.boolean
deleteLastFromRealm()
Deletes the last object from the Realm.E
first()
Gets the first object from the collection.E
first(E defaultValue)
Gets the first object from the collection.RealmList<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()
Checks if a collection has finished loading its data yet.boolean
isManaged()
Checks if the collection is managed by Realm.boolean
isValid()
Checks if the collection is still valid to use, i.e., theRealm
instance hasn't been closed.Iterator<E>
iterator()
E
last()
Gets the last object from the collection.E
last(E defaultValue)
Gets the last object from the collection.ListIterator<E>
listIterator()
ListIterator<E>
listIterator(int location)
boolean
load()
Blocks the collection until all data are available.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.void
move(int oldPos, int newPos)
Moves an object from one position to another, while maintaining a fixed sized list.E
remove(int location)
Removes the object at the specified location from this list.boolean
remove(Object object)
Removes one instance of the specified object from thisCollection
if one is contained.boolean
removeAll(Collection<?> collection)
Removes all occurrences in thisCollection
of each object in the specifiedCollection
.void
removeAllChangeListeners()
Removes all user-defined change listeners.void
removeChangeListener(OrderedRealmCollectionChangeListener<RealmList<E>> listener)
Removes the specified change listener.void
removeChangeListener(RealmChangeListener<RealmList<E>> listener)
Removes the specified change listener.E
set(int location, E object)
Replaces the element at the specified location in this list with the specified object.int
size()
Returns the number of elements in thisList
.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.String
toString()
RealmQuery<E>
where()
Returns a RealmQuery, which can be used to query for specific objects of this class.-
Methods inherited from class java.util.AbstractList
addAll, equals, hashCode, indexOf, lastIndexOf, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
addAll, addAll, containsAll, equals, hashCode, indexOf, isEmpty, lastIndexOf, replaceAll, retainAll, sort, spliterator, subList, toArray, toArray
-
-
-
-
Constructor Detail
-
RealmList
public RealmList()
Creates a RealmList in unmanaged mode, where the elements are not controlled by a Realm. This effectively makes the RealmList function as aArrayList
and it is not possible to query the objects in this state.Use
Realm.copyToRealm(Iterable, ImportFlag...)
to properly persist its elements in Realm.
-
RealmList
public RealmList(E... objects)
Creates a RealmList in unmanaged mode with an initial list of elements. A RealmList in unmanaged mode function as aArrayList
and it is not possible to query the objects in this state.Use
Realm.copyToRealm(Iterable, ImportFlag...)
to properly persist all unmanaged elements in Realm.- Parameters:
objects
- initial objects in the list.
-
-
Method Detail
-
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.
-
freeze
public RealmList<E> freeze()
- Specified by:
freeze
in interfaceio.realm.internal.Freezable<E>
-
isFrozen
public boolean isFrozen()
- Specified by:
isFrozen
in interfaceio.realm.internal.ManageableObject
-
isManaged
public boolean isManaged()
Checks if the collection is managed by Realm. A managed collection is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed collection will be live-updated so it always points to the latest data. Managed collections are thread confined so that they cannot be accessed from other threads than the one that created them.If this method returns
false
, the collection is unmanaged. An unmanaged collection is just a normal java collection, so it will not be live updated.- Specified by:
isManaged
in interfaceio.realm.internal.ManageableObject
- Specified by:
isManaged
in interfaceRealmCollection<E>
- Returns:
true
if this is a managedRealmCollection
,false
otherwise.
-
add
public void add(int location, @Nullable E element)
Inserts the specified object into this List at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this List, the object is added at the end.- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
Realm.copyToRealm(RealmModel, ImportFlag...)
afterwards. - Managed RealmLists: It is possible to add unmanaged objects to a RealmList that is already managed. In
that case the object will transparently be copied to Realm using
Realm.copyToRealm(RealmModel, ImportFlag...)
orRealm.copyToRealmOrUpdate(RealmModel, ImportFlag...)
if it has a primary key.
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Parameters:
location
- the index at which to insert.element
- the element to add.- Throws:
IllegalStateException
- if Realm instance has been closed or container object has been removed.IndexOutOfBoundsException
- iflocation < 0 || location > size()
.
- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
-
add
public boolean add(@Nullable E object)
Adds the specified object at the end of this List.- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
Realm.copyToRealm(RealmModel, ImportFlag...)
afterwards. - Managed RealmLists: It is possible to add unmanaged objects to a RealmList that is already managed. In
that case the object will transparently be copied to Realm using
Realm.copyToRealm(RealmModel, ImportFlag...)
orRealm.copyToRealmOrUpdate(RealmModel, ImportFlag...)
if it has a primary key.
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Parameters:
object
- the object to add.- Returns:
- always
true
. - Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.
- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
-
set
public E set(int location, @Nullable E object)
Replaces the element at the specified location in this list with the specified object.- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
Realm.copyToRealm(RealmModel, ImportFlag...)
afterwards. - Managed RealmLists: It is possible to add unmanaged objects to a RealmList that is already managed.
In that case the object will transparently be copied to Realm using
Realm.copyToRealm(RealmModel, ImportFlag...)
orRealm.copyToRealmOrUpdate(RealmModel, ImportFlag...)
if it has a primary key.
- Specified by:
set
in interfaceList<E>
- Overrides:
set
in classAbstractList<E>
- Parameters:
location
- the index at which to put the specified object.object
- the object to add.- Returns:
- the previous element at the index.
- Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.IndexOutOfBoundsException
- iflocation < 0 || location >= size()
.
- Unmanaged RealmLists: It is possible to add both managed and unmanaged objects. If adding managed
objects to an unmanaged RealmList they will not be copied to the Realm again if using
-
move
public void move(int oldPos, int newPos)
Moves an object from one position to another, while maintaining a fixed sized list. RealmObjects will be shifted so nonull
values are introduced.- Parameters:
oldPos
- index of RealmObject to move.newPos
- target position. If newPos < oldPos the object at the location will be shifted to the right. If oldPos < newPos, indexes > oldPos will be shifted once to the left.- Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.IndexOutOfBoundsException
- if any position is outside [0, size()].
-
clear
public void clear()
Removes all elements from this list, leaving it empty. This method doesn't remove the objects from the Realm.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
- Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.- See Also:
List.isEmpty()
,List.size()
,deleteAllFromRealm()
-
remove
public E remove(int location)
Removes the object at the specified location from this list.- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractList<E>
- Parameters:
location
- the index of the object to remove.- Returns:
- the removed object.
- Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.IndexOutOfBoundsException
- iflocation < 0 || location >= size()
.
-
remove
public boolean remove(@Nullable Object object)
Removes one instance of the specified object from thisCollection
if one is contained. This implementation iterates over thisCollection
and tests each elemente
returned by the iterator, whethere
is equal to the given object. Ifobject != null
then this test is performed usingobject.equals(e)
, otherwise usingobject == null
. If an element equal to the given object is found, then theremove
method is called on the iterator andtrue
is returned,false
otherwise. If the iterator does not support removing elements, anUnsupportedOperationException
is thrown.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
object
- the object to remove.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
ClassCastException
- if the object passed is not of the correct type.NullPointerException
- ifobject
isnull
.
-
removeAll
public boolean removeAll(Collection<?> collection)
Removes all occurrences in thisCollection
of each object in the specifiedCollection
. After this method returns none of the elements in the passedCollection
can be found in thisCollection
anymore.This implementation iterates over the
Collection
and tests each elemente
returned by the iterator, whether it is contained in the specifiedCollection
. If this test is positive, then theremove
method is called on the iterator.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollection<E>
- Parameters:
collection
- the collection of objects to remove.- Returns:
true
if thisCollection
is modified,false
otherwise.- Throws:
ClassCastException
- if one or more elements ofcollection
isn't of the correct type.NullPointerException
- ifcollection
isnull
.
-
deleteFirstFromRealm
public boolean deleteFirstFromRealm()
Deletes the first object from the Realm. This also removes it from this collection.- Specified by:
deleteFirstFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,false
otherwise.
-
deleteLastFromRealm
public boolean deleteLastFromRealm()
Deletes the last object from the Realm. This also removes it from this collection.- Specified by:
deleteLastFromRealm
in interfaceOrderedRealmCollection<E>
- Returns:
true
if an object was deleted,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:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.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.
-
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 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.
-
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.
-
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.
-
size
public int size()
Returns the number of elements in thisList
.- 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
List
. - Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.
-
where
public RealmQuery<E> where()
Returns a RealmQuery, which can be used to query for specific objects of this class.- Specified by:
where
in interfaceRealmCollection<E>
- Returns:
- a RealmQuery object.
- Throws:
IllegalStateException
- if Realm instance has been closed or parent object has been removed.- See Also:
RealmQuery
-
min
@Nullable 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.
-
max
@Nullable 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.
-
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.
-
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.
-
minDate
@Nullable 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.
-
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.
-
isLoaded
public boolean isLoaded()
Checks if a collection has finished loading its data yet.- Specified by:
isLoaded
in interfaceRealmCollection<E>
- Returns:
true
if data has been loaded and is available,false
if data is still being loaded.
-
load
public boolean load()
Blocks the collection until all data are available.- Specified by:
load
in interfaceRealmCollection<E>
- Returns:
true
if the data could be successfully loaded,false
otherwise.
-
contains
public boolean contains(@Nullable Object object)
Returnstrue
if the list contains the specified element when attached to a Realm. This method will query the native Realm underlying storage engine to quickly find the specified element.If the list is not attached to a Realm, the default
List.contains(Object)
implementation will occur.- 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 element whose presence in this list is to be tested.- Returns:
true
if this list contains the specified element otherwisefalse
.
-
listIterator
@Nonnull public ListIterator<E> listIterator()
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
-
listIterator
@Nonnull public ListIterator<E> listIterator(int location)
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
-
createSnapshot
public OrderedRealmCollectionSnapshot<E> createSnapshot()
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 list.- Returns:
Realm
instance this collection belongs to ornull
if the collection is unmanaged.- Throws:
IllegalStateException
- if the Realm is an instance ofDynamicRealm
or theRealm
was already closed.
-
toString
public String toString()
- Overrides:
toString
in classAbstractCollection<E>
-
asFlowable
public Flowable<RealmList<E>> asFlowable()
Returns an Rx Flowable that monitors changes to this RealmList. It will emit the current RealmList when subscribed to. RealmList will continually be emitted as the RealmList is 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 RealmList. This means that if you need to do further processing, it is recommend to observe the values on a computation scheduler:
list.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:list.asFlowable() .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.- See Also:
- RxJava and Realm
-
asChangesetObservable
public Observable<CollectionChange<RealmList<E>>> asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmList. It will emit the current RealmList when subscribed. For each update to the RealmList a pair consisting of the RealmList and theOrderedCollectionChangeSet
will be sent. The changeset will benull
the first time an RealmList is emitted.RealmList will continually be emitted as the RealmList is 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:
list.asChangesetObservable() .observeOn(Schedulers.computation()) .map((rxList, changes) -> doExpensiveWork(rxList, 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
-
addChangeListener
public void addChangeListener(OrderedRealmCollectionChangeListener<RealmList<E>> listener)
Adds a change listener to thisRealmList
.Registering a change listener will not prevent the underlying RealmList from being garbage collected. If the RealmList 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 RealmList<Dog> dogs; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dogs = realm.where(Person.class).findFirst().getDogs(); dogs.addChangeListener(new OrderedRealmCollectionChangeListener<RealmList<Dog>>() { \@Override public void onChange(RealmList<Dog> dogs, 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.
-
removeChangeListener
public void removeChangeListener(OrderedRealmCollectionChangeListener<RealmList<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
-
addChangeListener
public void addChangeListener(RealmChangeListener<RealmList<E>> listener)
Adds a change listener to thisRealmList
.Registering a change listener will not prevent the underlying RealmList from being garbage collected. If the RealmList 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 RealmList<Dog> dogs; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dogs = realm.where(Person.class).findFirst().getDogs(); dogs.addChangeListener(new RealmChangeListener<RealmList<Dog>>() { \@Override public void onChange(RealmList<Dog> dogs) { // 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.
-
removeChangeListener
public void removeChangeListener(RealmChangeListener<RealmList<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
-
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
-
-