Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ / /

Class RealmList

On this page

  • io.realm
  • Constructors
  • Field Summary
  • Method Summary
  • Inherited Methods
  • Field Detail
  • baseRealm
  • Constructor Detail
  • Method Detail
  • add
  • addChangeListener
  • asChangesetObservable
  • asFlowable
  • average
  • clear
  • contains
  • createSnapshot
  • deleteAllFromRealm
  • deleteFirstFromRealm
  • deleteFromRealm
  • deleteLastFromRealm
  • first
  • freeze
  • get
  • getRealm
  • isFrozen
  • isLoaded
  • isManaged
  • isValid
  • iterator
  • last
  • listIterator
  • load
  • max
  • maxDate
  • min
  • minDate
  • move
  • remove
  • removeAll
  • removeAllChangeListeners
  • removeChangeListener
  • set
  • size
  • sort
  • sum
  • toString
  • where
java.lang.Object

Implemented interfaces:

RealmList is used to model one-to-many relationships in a io.realm.RealmObject . 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 io.realm.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 than Integer.MAX_VALUE . In that case, you can access only first Integer.MAX_VALUE elements in it.

Constructor and Description

Creates a RealmList in unmanaged mode, where the elements are not controlled by a Realm.

E[] objects
)

Creates a RealmList in unmanaged mode with an initial list of elements.

Modifier and Type
Field and Description
public final BaseRealm

baseRealm

The BaseRealm instance in which this list resides.

Warning: This field is only exposed for internal usage, and should not be used.

Modifier and Type
Method and Description
public boolean
add (
E object
)

Adds the specified object at the end of this List.

public void
add (
int location,
E element
)

Inserts the specified object into this List at the specified location.

public void
public void
public <any>

Returns an Rx Observable that monitors changes to this RealmList.

public <any>

Returns an Rx Flowable that monitors changes to this RealmList.

public double
String fieldName
)

Returns the average of a given field.

public void

Removes all elements from this list, leaving it empty.

public boolean
Object object
)

Returns true if the list contains the specified element when attached to a Realm.

Creates a snapshot from this OrderedRealmCollection .

public boolean

This deletes all objects in the collection from the underlying Realm as well as from the collection.

public boolean

Deletes the first object from the Realm.

public void
int location
)

Deletes the object at the given index from the Realm.

public boolean

Deletes the last object from the Realm.

public E
E defaultValue
)

Gets the first object from the collection.

public E

Gets the first object from the collection.

public RealmList
public E
get (
int location
)

Returns the element at the specified location in this list.

public Realm

Returns the Realm instance to which this collection belongs.

public boolean
public boolean

Checks if a collection has finished loading its data yet.

public boolean

Checks if the collection is managed by Realm.

public boolean

Checks if the collection is still valid to use, i.e., the io.realm.Realm instance hasn't been closed.

public Iterator
public E
E defaultValue
)

Gets the last object from the collection.

public E
last ()

Gets the last object from the collection.

int location
)
public boolean
load ()

Blocks the collection until all data are available.

public Number
max (
String fieldName
)

Finds the maximum value of a field.

public Date
String fieldName
)

Finds the maximum date.

public Number
min (
String fieldName
)

Finds the minimum value of a field.

public Date
String fieldName
)

Finds the minimum date.

public void
int oldPos,
int newPos
)

Moves an object from one position to another, while maintaining a fixed sized list.

public boolean
Object object
)

Removes one instance of the specified object from this Collection if one is contained.

public E
int location
)

Removes the object at the specified location from this list.

public boolean

Removes all occurrences in this Collection of each object in the specified Collection .

public void

Removes all user-defined change listeners.

public void

Removes the specified change listener.

public void
public E
set (
int location,
E object
)

Replaces the element at the specified location in this list with the specified object.

public int
size ()

Returns the number of elements in this List .

java.lang.String[] fieldNames,
io.realm.Sort[] sortOrders
)

Sorts a collection based on the provided fields and sort orders.

String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2
)

Sorts a collection based on the provided fields and sort orders.

String fieldName,
Sort sortOrder
)

Sorts a collection based on the provided field and sort order.

String fieldName
)

Sorts a collection based on the provided field in ascending order.

public Number
sum (
String fieldName
)

Calculates the sum of a given field.

public String
public RealmQuery

Returns a RealmQuery, which can be used to query for specific objects of this class.

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize

  • Methods inherited from class java.util.AbstractCollection : iterator , size , isEmpty , contains , toArray , toArray , add , remove , containsAll , addAll , removeAll , retainAll , clear , toString

  • Methods inherited from class java.util.AbstractList : add , get , set , add , remove , indexOf , lastIndexOf , clear , addAll , iterator , listIterator , listIterator , subList , equals , hashCode , removeRange

The BaseRealm instance in which this list resides.

Warning: This field is only exposed for internal usage, and should not be used.

public RealmList ()

Creates a RealmList in unmanaged mode, where the elements are not controlled by a Realm. This effectively makes the RealmList function as a java.util.ArrayList and it is not possible to query the objects in this state.

Use io.realm.Realm.copyToRealm(Iterable, ImportFlag...) to properly persist its elements in Realm.

public RealmList (
E[] objects
)

Creates a RealmList in unmanaged mode with an initial list of elements. A RealmList in unmanaged mode function as a java.util.ArrayList and it is not possible to query the objects in this state.

Use io.realm.Realm.copyToRealm(Iterable, ImportFlag...) to properly persist all unmanaged elements in Realm.

Parameters

  • objects - initial objects in the list.

public boolean add (
E object
)

Adds the specified object at the end of this List.

  1. 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.

  2. 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...) or Realm.copyToRealmOrUpdate(RealmModel, ImportFlag...) if it has a primary key.

Parameters

  • object - the object to add.

Returns

always true .

Throws

Overrides

add in class AbstractList

public void add (
int location,
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.

  1. 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.

  2. 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...) or Realm.copyToRealmOrUpdate(RealmModel, ImportFlag...) if it has a primary key.

Parameters

  • location - the index at which to insert.

  • element - the element to add.

Throws

Overrides

add in class AbstractList

Adds a change listener to this RealmList .

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

Adds a change listener to this RealmList .

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

public <any> 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 the OrderedCollectionChangeSet will be sent. The changeset will be null 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 call onComplete or OnError .

Throws

Tip

See also:

public <any> 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 the first() operator:

list.asFlowable()
.first()
.subscribe( ... ) // You only get the results once

Returns

RxJava Observable that only calls onNext . It will never call onComplete or OnError .

Throws

  • UnsupportedOperationException - if the required RxJava framework is not on the classpath or the corresponding Realm instance doesn't support RxJava.

Tip

See also:

public double average (
String fieldName
)

Returns the average of a given field.

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 with null values are ignored.

public void clear ()

Removes all elements from this list, leaving it empty. This method doesn't remove the objects from the Realm.

Throws

Overrides

clear in class AbstractList

public boolean contains (
Object object
)

Returns true 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.

Parameters

  • object - the element whose presence in this list is to be tested.

Returns

true if this list contains the specified element otherwise false .

Overrides

contains in class AbstractCollection

Creates a snapshot from this OrderedRealmCollection .

Returns

the snapshot of this collection.

public boolean deleteAllFromRealm ()

This deletes all objects in the collection from the underlying Realm as well as from the collection.

Returns

true if objects was deleted, false otherwise.

public boolean deleteFirstFromRealm ()

Deletes the first object from the Realm. This also removes it from this collection.

Returns

true if an object was deleted, false otherwise.

public void deleteFromRealm (
int location
)
Deletes the object at the given index from the Realm. This also removes it from the collection.
public boolean deleteLastFromRealm ()

Deletes the last object from the Realm. This also removes it from this collection.

Returns

true if an object was deleted, false otherwise.

public E first (
E defaultValue
)

Gets the first object from the collection. If the collection is empty, the provided default will be used instead.

Returns

the first object or the provided default.

public E first ()

Gets the first object from the collection.

Returns

the first object.

public RealmList freeze ()

public E get (
int location
)

Returns the element at the specified location in this list.

Parameters

  • location - the index of the element to return.

Returns

the element at the specified index.

Throws

Overrides

get in class AbstractList

public Realm getRealm ()

Returns the Realm instance to which this collection belongs.

Calling Realm.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 or null if the collection is unmanaged.

Throws

public boolean isFrozen ()

public boolean isLoaded ()

Checks if a collection has finished loading its data yet.

Returns

true if data has been loaded and is available, false if data is still being loaded.

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.

Returns

true if this is a managed RealmCollection , false otherwise.

public boolean isValid ()

Checks if the collection is still valid to use, i.e., the io.realm.Realm instance hasn't been closed. It will always return true for an unmanaged collection.

Returns

true if it is still valid to use or an unmanaged collection, false otherwise.

public Iterator iterator ()

Overrides

iterator in class AbstractList

public E last (
E defaultValue
)

Gets the last object from the collection. If the collection is empty, the provided default will be used instead.

Returns

the last object or the provided default.

public E last ()

Gets the last object from the collection.

Returns

the last object.

int location
)

Overrides

listIterator in class AbstractList

Overrides

listIterator in class AbstractList

public boolean load ()

Blocks the collection until all data are available.

Returns

true if the data could be successfully loaded, false otherwise.

public Number max (
String fieldName
)

Finds the maximum value of a field.

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 with null values are ignored.

public Date maxDate (
String fieldName
)

Finds the maximum date.

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 with null values are ignored.

public Number min (
String fieldName
)

Finds the minimum value of a field.

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 with null values are ignored.

public Date minDate (
String fieldName
)

Finds the minimum date.

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 with null values are ignored.

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 no null 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

public boolean remove (
Object object
)

Removes one instance of the specified object from this Collection if one is contained. This implementation iterates over this Collection and tests each element e returned by the iterator, whether e is equal to the given object. If object != null then this test is performed using object.equals(e) , otherwise using object == null . If an element equal to the given object is found, then the remove method is called on the iterator and true is returned, false otherwise. If the iterator does not support removing elements, an UnsupportedOperationException is thrown.

Parameters

  • object - the object to remove.

Returns

true if this Collection is modified, false otherwise.

Throws

Overrides

remove in class AbstractCollection

public E remove (
int location
)

Removes the object at the specified location from this list.

Parameters

  • location - the index of the object to remove.

Returns

the removed object.

Throws

Overrides

remove in class AbstractList

public boolean removeAll (
)

Removes all occurrences in this Collection of each object in the specified Collection . After this method returns none of the elements in the passed Collection can be found in this Collection anymore.

This implementation iterates over the Collection and tests each element e returned by the iterator, whether it is contained in the specified Collection . If this test is positive, then the remove method is called on the iterator.

Parameters

  • collection - the collection of objects to remove.

Returns

true if this Collection is modified, false otherwise.

Throws

Overrides

removeAll in class AbstractCollection

Removes all user-defined change listeners.

Throws

Removes the specified change listener.

Parameters

  • listener - the change listener to be removed.

Throws

Removes the specified change listener.

Parameters

  • listener - the change listener to be removed.

Throws

public E set (
int location,
E object
)

Replaces the element at the specified location in this list with the specified object.

  1. 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.

  2. 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...) or Realm.copyToRealmOrUpdate(RealmModel, ImportFlag...) if it has a primary key.

Parameters

  • location - the index at which to put the specified object.

  • object - the object to add.

Returns

the previous element at the index.

Throws

Overrides

set in class AbstractList

public int size ()

Returns the number of elements in this List .

Returns

the number of elements in this List .

Throws

Overrides

size in class AbstractCollection

java.lang.String[] fieldNames,
io.realm.Sort[] sortOrders
)

Sorts a collection based on the provided fields and sort orders.

Returns

a new sorted RealmResults will be created and returned. The original collection stays unchanged.

String fieldName1,
Sort sortOrder1,
String fieldName2,
Sort sortOrder2
)

Sorts a collection based on the provided fields and sort orders.

Returns

a new sorted RealmResults will be created and returned. The original collection stays unchanged.

String fieldName,
Sort sortOrder
)

Sorts a collection based on the provided field and sort order.

Returns

a new sorted RealmResults will be created and returned. The original collection stays unchanged.

String fieldName
)

Sorts a collection based on the provided field in ascending order.

Returns

a new sorted RealmResults will be created and returned. The original collection stays unchanged.

public Number sum (
String fieldName
)

Calculates the sum of a given field.

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 with null values are ignored.

public String toString ()

Overrides

toString in class AbstractCollection

public RealmQuery where ()

Returns a RealmQuery, which can be used to query for specific objects of this class.

Returns

a RealmQuery object.

Throws

Tip

See also:

Back

RealmFieldType