Class RealmObject
- java.lang.Object
-
- io.realm.RealmObject
-
- All Implemented Interfaces:
io.realm.internal.ManageableObject
,RealmModel
- Direct Known Subclasses:
DynamicRealmObject
public abstract class RealmObject extends Object implements RealmModel, io.realm.internal.ManageableObject
In Realm you define your RealmObject classes by sub-classing RealmObject and adding fields to be persisted. You then create your objects within a Realm, and use your custom subclasses instead of using the RealmObject class directly.An annotation processor will create a proxy class for your RealmObject subclass.
The following field data types are supported:
- boolean/Boolean
- short/Short
- int/Integer
- long/Long
- float/Float
- double/Double
- byte[]
- String
- Date
- UUID
- org.bson.types.Decimal128
- org.bson.types.ObjectId
- Any RealmObject subclass
- RealmList
- RealmDictionary
The types
short
,int
, andlong
are mapped tolong
when storing within a Realm.The only restriction a RealmObject has is that fields are not allowed to be final or volatile. Any method as well as public fields are allowed. When providing custom constructors, a public constructor with no arguments must be declared.
Fields annotated with
Ignore
don't have these restrictions and don't require either a getter or setter.Realm will create indexes for fields annotated with
Index
. This will speedup queries but will have a negative impact on inserts and updates.A RealmObject cannot be passed between different threads.
-
-
Constructor Summary
Constructors Constructor Description RealmObject()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <E extends RealmModel>
voidaddChangeListener(E object, RealmChangeListener<E> listener)
Adds a change listener to a RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.static <E extends RealmModel>
voidaddChangeListener(E object, RealmObjectChangeListener<E> listener)
Adds a change listener to a RealmObject to get detailed information about the changes.<E extends RealmModel>
voidaddChangeListener(RealmChangeListener<E> listener)
Adds a change listener to this RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.<E extends RealmModel>
voidaddChangeListener(RealmObjectChangeListener<E> listener)
Adds a change listener to this RealmObject to get detailed information about changes.<E extends RealmObject>
Observable<ObjectChange<E>>asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmObject.static <E extends RealmModel>
Observable<ObjectChange<E>>asChangesetObservable(E object)
Returns an Rx Observable that monitors changes to this RealmObject.<E extends RealmObject>
Flowable<E>asFlowable()
Returns an RxJava Flowable that monitors changes to this RealmObject.static <E extends RealmModel>
Flowable<E>asFlowable(E object)
Returns an RxJava Flowable that monitors changes to this RealmObject.void
deleteFromRealm()
Deletes the object from the Realm it is currently associated to.static <E extends RealmModel>
voiddeleteFromRealm(E object)
Deletes the object from the Realm it is currently associated with.<E extends RealmModel>
Efreeze()
Returns a frozen snapshot of this object.static <E extends RealmModel>
Efreeze(E object)
Returns a frozen snapshot of this object.Realm
getRealm()
ReturnsRealm
instance where thisRealmObject
belongs.static Realm
getRealm(RealmModel model)
returnsRealm
instance where themodel
belongs.boolean
isFrozen()
Returns whether or not this RealmObject is frozen.static <E extends RealmModel>
booleanisFrozen(E object)
Returns whether or not this RealmObject is frozen.boolean
isLoaded()
Checks if the query used to find this RealmObject has completed.static <E extends RealmModel>
booleanisLoaded(E object)
Checks if the query used to find this RealmObject has completed.boolean
isManaged()
Checks if this object is managed by Realm.static <E extends RealmModel>
booleanisManaged(E object)
Checks if this object is managed by Realm.boolean
isValid()
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has theRealm
been closed.static <E extends RealmModel>
booleanisValid(E object)
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has theRealm
been closed.boolean
load()
Makes an asynchronous query blocking.static <E extends RealmModel>
booleanload(E object)
Makes an asynchronous query blocking.void
removeAllChangeListeners()
Removes all registered listeners.static <E extends RealmModel>
voidremoveAllChangeListeners(E object)
Removes all registered listeners from the given RealmObject.static <E extends RealmModel>
voidremoveChangeListener(E object, RealmChangeListener<E> listener)
Removes a previously registered listener on the given RealmObject.static <E extends RealmModel>
voidremoveChangeListener(E object, RealmObjectChangeListener listener)
Removes a previously registered listener on the given RealmObject.void
removeChangeListener(RealmChangeListener listener)
Removes a previously registered listener.void
removeChangeListener(RealmObjectChangeListener listener)
Removes a previously registered listener.
-
-
-
Method Detail
-
deleteFromRealm
public final void deleteFromRealm()
Deletes the object from the Realm it is currently associated to.After this method is called the object will be invalid and any operation (read or write) performed on it will fail with an IllegalStateException.
- Throws:
IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.- See Also:
isValid()
-
deleteFromRealm
public static <E extends RealmModel> void deleteFromRealm(E object)
Deletes the object from the Realm it is currently associated with.After this method is called the object will be invalid and any operation (read or write) performed on it will fail with an IllegalStateException.
- Throws:
IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.- See Also:
isValid()
-
isValid
public final boolean isValid()
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has theRealm
been closed. It will always returntrue
for unmanaged objects.Note that this can be used to check the validity of certain conditions such as being
null
when observed.realm.where(BannerRealm.class).equalTo("type", type).findFirstAsync().asFlowable() .filter(result.isLoaded() && result.isValid()) .first()
- Specified by:
isValid
in interfaceio.realm.internal.ManageableObject
- Returns:
true
if the object is still accessible or an unmanaged object,false
otherwise.- See Also:
- Examples using Realm with RxJava
-
isValid
public static <E extends RealmModel> boolean isValid(@Nullable E object)
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has theRealm
been closed. It will always returntrue
for unmanaged objects.- Parameters:
object
- RealmObject to check validity for.- Returns:
true
if the object is still accessible or an unmanaged object,false
otherwise.
-
isFrozen
public final boolean isFrozen()
Returns whether or not this RealmObject is frozen.- Specified by:
isFrozen
in interfaceio.realm.internal.ManageableObject
- Returns:
true
if the RealmObject is frozen,false
if it is not.- See Also:
freeze()
-
freeze
public final <E extends RealmModel> E freeze()
Returns a frozen snapshot of this object. The frozen copy can be read and queried from any thread without throwing anIllegalStateException
.Freezing a RealmObject also creates a frozen Realm which has its own lifecycle, but if the live Realm that spawned the original collection is fully closed (i.e. all instances across all threads are closed), the frozen Realm and object will be closed as well.
Frozen objects can be queried as normal, but trying to mutate it in any way or attempting to register a listener will throw an
IllegalStateException
.Note: Keeping a large number of frozen objects with different versions alive can have a negative impact on the filesize of the Realm. In order to avoid such a situation it is possible to set
RealmConfiguration.Builder.maxNumberOfActiveVersions(long)
.- Returns:
- a frozen copy of this object.
- Throws:
IllegalStateException
- if this method is called from inside a write transaction.
-
isFrozen
public static <E extends RealmModel> boolean isFrozen(E object)
Returns whether or not this RealmObject is frozen.- Returns:
true
if the RealmObject is frozen,false
if it is not.- See Also:
freeze()
-
freeze
public static <E extends RealmModel> E freeze(E object)
Returns a frozen snapshot of this object. The frozen copy can be read and queried from any thread without throwing anIllegalStateException
.Freezing a RealmObject also creates a frozen Realm which has its own lifecycle, but if the live Realm that spawned the original collection is fully closed (i.e. all instances across all threads are closed), the frozen Realm and object will be closed as well.
Frozen objects can be queried as normal, but trying to mutate it in any way or attempting to register a listener will throw an
IllegalStateException
.Note: Keeping a large number of frozen objects with different versions alive can have a negative impact on the filesize of the Realm. In order to avoid such a situation it is possible to set
RealmConfiguration.Builder.maxNumberOfActiveVersions(long)
.- Returns:
- a frozen copy of this object.
- Throws:
IllegalStateException
- if this method is called from inside a write transaction.
-
isLoaded
public final boolean isLoaded()
Checks if the query used to find this RealmObject has completed.Async methods like
RealmQuery.findFirstAsync()
return anRealmObject
that represents the future result of theRealmQuery
. It can be considered similar to aFuture
in this regard.Once
isLoaded()
returnstrue
, the object represents the query result even if the query didn't find any object matching the query parameters. In this case theRealmObject
will become a "null" object."Null" objects represents
null
. An exception is throw if any accessor is called, so it is important to also checkisValid()
before calling any methods. A common pattern is:Person person = realm.where(Person.class).findFirstAsync(); person.isLoaded(); // == false person.addChangeListener(new RealmChangeListener() { \@Override public void onChange(Person person) { person.isLoaded(); // Always true here if (person.isValid()) { // It is safe to access the person. } } });
Synchronous RealmObjects are by definition blocking hence this method will always return
true
for them. This method will returntrue
if called on an unmanaged object (created outside of Realm).- Returns:
true
if the query has completed,false
if the query is in progress.- See Also:
isValid()
-
isLoaded
public static <E extends RealmModel> boolean isLoaded(E object)
Checks if the query used to find this RealmObject has completed.Async methods like
RealmQuery.findFirstAsync()
return anRealmObject
that represents the future result of theRealmQuery
. It can be considered similar to aFuture
in this regard.Once
isLoaded()
returnstrue
, the object represents the query result even if the query didn't find any object matching the query parameters. In this case theRealmObject
will become a "null" object."Null" objects represents
null
. An exception is throw if any accessor is called, so it is important to also checkisValid()
before calling any methods. A common pattern is:Person person = realm.where(Person.class).findFirstAsync(); RealmObject.isLoaded(person); // == false RealmObject.addChangeListener(person, new RealmChangeListener() { \@Override public void onChange(Person person) { RealmObject.isLoaded(person); // always true here if (RealmObject.isValid(person)) { // It is safe to access the person. } } });
Synchronous RealmObjects are by definition blocking hence this method will always return
true
for them. This method will returntrue
if called on an unmanaged object (created outside of Realm).- Parameters:
object
- RealmObject to check.- Returns:
true
if the query has completed,false
if the query is in progress.- See Also:
isValid(RealmModel)
-
isManaged
public boolean isManaged()
Checks if this object is managed by Realm. A managed object is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed object will be live-updated so it always points to the latest data. It is possible to register a change listener usingaddChangeListener(RealmChangeListener)
to be notified when changes happen. Managed objects are thread confined so that they cannot be accessed from other threads than the one that created them.If this method returns
false
, the object is unmanaged. An unmanaged object is just a normal Java object, so it can be parsed freely across threads, but the data in the object is not connected to the underlying Realm, so it will not be live updated.It is possible to create a managed object from an unmanaged object by using
Realm.copyToRealm(RealmModel, ImportFlag...)
. An unmanaged object can be created from a managed object by usingRealm.copyFromRealm(RealmModel)
.- Specified by:
isManaged
in interfaceio.realm.internal.ManageableObject
- Returns:
true
if the object is managed,false
if it is unmanaged.
-
isManaged
public static <E extends RealmModel> boolean isManaged(E object)
Checks if this object is managed by Realm. A managed object is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed object will be live-updated so it always points to the latest data. It is possible to register a change listener usingaddChangeListener(RealmModel, RealmChangeListener)
to be notified when changes happen. Managed objects are thread confined so that they cannot be accessed from other threads than the one that created them.If this method returns
false
, the object is unmanaged. An unmanaged object is just a normal Java object, so it can be parsed freely across threads, but the data in the object is not connected to the underlying Realm, so it will not be live updated.It is possible to create a managed object from an unmanaged object by using
Realm.copyToRealm(RealmModel, ImportFlag...)
. An unmanaged object can be created from a managed object by usingRealm.copyFromRealm(RealmModel)
.- Returns:
true
if the object is managed,false
if it is unmanaged.
-
getRealm
public Realm getRealm()
ReturnsRealm
instance where thisRealmObject
belongs.You must not call
Closeable.close()
against returned instance.- Returns:
Realm
instance where this object belongs to ornull
if this object is unmanaged.- Throws:
IllegalStateException
- if this object is an instance ofDynamicRealmObject
or this object was already deleted or the correspondingRealm
was already closed.
-
getRealm
public static Realm getRealm(RealmModel model)
returnsRealm
instance where themodel
belongs.You must not call
Closeable.close()
against returned instance.- Parameters:
model
- anRealmModel
instance other thanDynamicRealmObject
.- Returns:
Realm
instance where themodel
belongs ornull
if themodel
is unmanaged.- Throws:
IllegalArgumentException
- if themodel
isnull
.IllegalStateException
- if themodel
is an instance ofDynamicRealmObject
or this object was already deleted or the correspondingRealm
was already closed.
-
load
public final boolean load()
Makes an asynchronous query blocking. This will also trigger any registered listeners.Note: This will return
true
if called for an unmanaged object (created outside of Realm).- Returns:
true
if it successfully completed the query,false
otherwise.
-
load
public static <E extends RealmModel> boolean load(E object)
Makes an asynchronous query blocking. This will also trigger any registered listeners.Note: This will return
true
if called for an unmanaged object (created outside of Realm).- Parameters:
object
- RealmObject to force load.- Returns:
true
if it successfully completed the query,false
otherwise.
-
addChangeListener
public final <E extends RealmModel> void addChangeListener(RealmObjectChangeListener<E> listener)
Adds a change listener to this RealmObject to get detailed information about changes. The listener will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject 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 Person person; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); person = realm.where(Person.class).findFirst(); person.addChangeListener(new RealmObjectChangeListener<Person>() { \@Override public void onChange(Person person, ObjectChangeSet changeSet) { // React to change } }); } }
- Parameters:
listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if the change listener isnull
or the object is an unmanaged object.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.IllegalStateException
- if you try to add a listener inside a transaction.
-
addChangeListener
public final <E extends RealmModel> void addChangeListener(RealmChangeListener<E> listener)
Adds a change listener to this RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject 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 Person person; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); person = realm.where(Person.class).findFirst(); person.addChangeListener(new RealmChangeListener<Person>() { \@Override public void onChange(Person person) { // React to change } }); } }
- Parameters:
listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if the change listener isnull
or the object is an unmanaged object.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.IllegalStateException
- if you try to add a listener inside a transaction.
-
addChangeListener
public static <E extends RealmModel> void addChangeListener(E object, RealmObjectChangeListener<E> listener)
Adds a change listener to a RealmObject to get detailed information about the changes. The listener will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject 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 Person person; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); person = realm.where(Person.class).findFirst(); person.addChangeListener(new RealmObjectChangeListener<Person>() { \@Override public void onChange(Person person, ObjectChangeSet changeSet) { // React to change } }); } }
- Parameters:
object
- RealmObject to add listener to.listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if theobject
isnull
or an unmanaged object, or the change listener isnull
.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.IllegalStateException
- if you try to add a listener inside a transaction.
-
addChangeListener
public static <E extends RealmModel> void addChangeListener(E object, RealmChangeListener<E> listener)
Adds a change listener to a RealmObject that will be triggered if any value field or referenced RealmObject field is changed, or the RealmList field itself is changed.Registering a change listener will not prevent the underlying RealmObject from being garbage collected. If the RealmObject 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 Person person; // Strong reference to keep listeners alive \@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); person = realm.where(Person.class).findFirst(); person.addChangeListener(new RealmChangeListener<Person>() { \@Override public void onChange(Person person) { // React to change } }); } }
- Parameters:
object
- RealmObject to add listener to.listener
- the change listener to be notified.- Throws:
IllegalArgumentException
- if theobject
isnull
or an unmanaged object, or the change listener isnull
.IllegalStateException
- if you try to add a listener from a non-Looper orIntentService
thread.IllegalStateException
- if you try to add a listener inside a transaction.
-
removeChangeListener
public final void removeChangeListener(RealmObjectChangeListener listener)
Removes a previously registered listener.- Parameters:
listener
- the instance to be removed.- Throws:
IllegalArgumentException
- if the change listener isnull
or the object is an unmanaged object.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.
-
removeChangeListener
public final void removeChangeListener(RealmChangeListener listener)
Removes a previously registered listener.- Parameters:
listener
- the instance to be removed.- Throws:
IllegalArgumentException
- if the change listener isnull
or the object is an unmanaged object.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.
-
removeChangeListener
public static <E extends RealmModel> void removeChangeListener(E object, RealmObjectChangeListener listener)
Removes a previously registered listener on the given RealmObject.- Parameters:
object
- RealmObject to remove listener from.listener
- the instance to be removed.- Throws:
IllegalArgumentException
- if theobject
or the change listener isnull
.IllegalArgumentException
- if object is an unmanaged RealmObject.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.
-
removeChangeListener
public static <E extends RealmModel> void removeChangeListener(E object, RealmChangeListener<E> listener)
Removes a previously registered listener on the given RealmObject.- Parameters:
object
- RealmObject to remove listener from.listener
- the instance to be removed.- Throws:
IllegalArgumentException
- if theobject
or the change listener isnull
.IllegalArgumentException
- if object is an unmanaged RealmObject.IllegalStateException
- if you try to remove a listener from a non-Looper Thread.
-
removeAllChangeListeners
public final void removeAllChangeListeners()
Removes all registered listeners.
-
removeAllChangeListeners
public static <E extends RealmModel> void removeAllChangeListeners(E object)
Removes all registered listeners from the given RealmObject.- Parameters:
object
- RealmObject to remove all listeners from.- Throws:
IllegalArgumentException
- if object isnull
or isn't managed by Realm.
-
asFlowable
public final <E extends RealmObject> Flowable<E> asFlowable()
Returns an RxJava Flowable that monitors changes to this RealmObject. It will emit the current object when subscribed to. Object updates will continually be emitted as the RealmObject is updated -onComplete
will never be called.When chaining a RealmObject flowable use
obj.<MyRealmObjectClass>asFlowable()
to pass on type information, otherwise the type of the following observables will beRealmObject
.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 Realm. This means that if you need to do further processing, it is recommend to observe the values on a computation scheduler:
obj.asFlowable() .observeOn(Schedulers.computation()) .map((rxObj) -> doExpensiveWork(rxObj)) .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:obj.asFlowable() .filter(obj -> obj.isLoaded()) .first() .subscribe( ... ) // You only get the object once
- Type Parameters:
E
- RealmObject class that is being observed. Must be this class or its super types.- 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 final <E extends RealmObject> Observable<ObjectChange<E>> asChangesetObservable()
Returns an Rx Observable that monitors changes to this RealmObject. It will emit the current RealmObject when subscribed to. For each update to the RealmObject a pair consisting of the RealmObject and theObjectChangeSet
will be sent. The changeset will benull
the first time the RealmObject is emitted.The RealmObject will continually be emitted as it 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:
obj.asChangesetObservable() .observeOn(Schedulers.computation()) .map((rxObj, changes) -> doExpensiveWork(rxObj, 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
-
asFlowable
public static <E extends RealmModel> Flowable<E> asFlowable(E object)
Returns an RxJava Flowable that monitors changes to this RealmObject. It will emit the current object when subscribed to. Object updates will continuously be emitted as the RealmObject is updated -onComplete
will never be called.When chaining a RealmObject observable use
obj.<MyRealmObjectClass>asFlowable()
to pass on type information, otherwise the type of the following observables will beRealmObject
.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 Realm. This means that if you need to do further processing, it is recommend to observe the values on a computation scheduler:
obj.asFlowable() .observeOn(Schedulers.computation()) .map((rxObj) -> doExpensiveWork(rxObj)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( ... );
If you would like the
asFlowable()
to stop emitting items you can instruct RxJava to emit only the first item by using thefirst()
operator:obj.asFlowable() .filter(obj -> obj.isLoaded()) .first() .subscribe( ... ) // You only get the object once
- Parameters:
object
- RealmObject class that is being observed. Must be this class or its super types.- Returns:
- RxJava Observable that only calls
onNext
. It will never callonComplete
orOnError
. - Throws:
UnsupportedOperationException
- if the required RxJava framework is not on the classpath.IllegalStateException
- if the Realm wasn't opened on a Looper thread.- See Also:
- RxJava and Realm
-
asChangesetObservable
public static <E extends RealmModel> Observable<ObjectChange<E>> asChangesetObservable(E object)
Returns an Rx Observable that monitors changes to this RealmObject. It will emit the current RealmObject when subscribed to. For each update to the RealmObject a pair consisting of the RealmObject and theObjectChangeSet
will be sent. The changeset will benull
the first time the RealmObject is emitted.The RealmObject will continually be emitted as it 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:
obj.asChangesetObservable() .observeOn(Schedulers.computation()) .map((rxObj, changes) -> doExpensiveWork(rxObj, changeså)) .observeOn(AndroidSchedulers.mainThread()) .subscribe( ... );
- Parameters:
object
- RealmObject class that is being observed. Must be this class or its super types.- 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
-
-