Class RealmObject
On this page
io.realm
Implemented interfaces:
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
, and long
are mapped to long
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 io.realm.annotations.Ignore don't have these restrictions and don't require either a getter or setter.
Realm will create indexes for fields annotated with io.realm.annotations.Index . This will speedup queries but will have a negative impact on inserts and updates.
A RealmObject cannot be passed between different threads.
Constructors
Constructor and Description |
---|
RealmObject () |
Method Summary
Modifier and Type | Method and Description |
---|---|
public static void | 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. |
public static void | Adds a change listener to a RealmObject to get detailed information about the changes. |
public final void | 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. |
public final void | Adds a change listener to this RealmObject to get detailed information about changes. |
public static <any> | Returns an Rx Observable that monitors changes to this RealmObject. |
public final <any> | asChangesetObservable <E >() Returns an Rx Observable that monitors changes to this RealmObject. |
public static <any> | Returns an RxJava Flowable that monitors changes to this RealmObject. |
public final <any> | asFlowable <E >() Returns an RxJava Flowable that monitors changes to this RealmObject. |
public static void | Deletes the object from the Realm it is currently associated with. |
public final void | Deletes the object from the Realm it is currently associated to. |
public static E | Returns a frozen snapshot of this object. |
public final E | freeze <E >() Returns a frozen snapshot of this object. |
public static Realm | returns Realm instance where the |
public Realm | getRealm () Returns Realm instance where this RealmObject belongs. |
public static boolean | Returns whether or not this RealmObject is frozen. |
public final boolean | isFrozen () Returns whether or not this RealmObject is frozen. |
public static boolean | Checks if the query used to find this RealmObject has completed. |
public final boolean | isLoaded () Checks if the query used to find this RealmObject has completed. |
public static boolean | Checks if this object is managed by Realm. |
public boolean | isManaged () Checks if this object is managed by Realm. |
public static boolean | Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the io.realm.Realm been closed. |
public final boolean | isValid () Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the io.realm.Realm been closed. |
public static boolean | Makes an asynchronous query blocking. |
public final boolean | load () Makes an asynchronous query blocking. |
public static void | Removes all registered listeners from the given RealmObject. |
public final void | Removes all registered listeners. |
public static void | Removes a previously registered listener on the given RealmObject. |
public static void | Removes a previously registered listener on the given RealmObject. |
public final void | Removes a previously registered listener. |
public final void | Removes a previously registered listener. |
Inherited Methods
Methods inherited from class java.lang.Object :
getClass
,hashCode
,equals
,clone
,toString
,notify
,notifyAll
,wait
,wait
,wait
,finalize
Constructor Detail
public RealmObject () |
---|
Method Detail
addChangeListener
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.
Parameters
Throws
|
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.
Parameters
Throws
|
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.
Parameters
Throws
|
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.
Parameters
Throws
|
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 the ObjectChangeSet will be sent. The changeset will be The RealmObject will continually be emitted as it is updated - 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:
Parameters
Returns RxJava Observable that only calls Throws
|
public final <any> asChangesetObservable <E >() | |||||
---|---|---|---|---|---|
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 the ObjectChangeSet will be sent. The changeset will be The RealmObject will continually be emitted as it is updated - 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:
Returns RxJava Observable that only calls Throws
|
asFlowable
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 - When chaining a RealmObject observable use 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:
If you would like the
Parameters
Returns RxJava Observable that only calls Throws
|
public final <any> asFlowable <E >() | |||||||||
---|---|---|---|---|---|---|---|---|---|
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 - When chaining a RealmObject flowable use 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:
If you would like the
Type Parameters
Returns RxJava Observable that only calls Throws
|
deleteFromRealm
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
|
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
|
freeze
Returns a frozen snapshot of this object. The frozen copy can be read and queried from any thread without throwing an IllegalStateException . 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
|
public final E freeze <E >() |
---|
Returns a frozen snapshot of this object. The frozen copy can be read and queried from any thread without throwing an IllegalStateException . 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
|
getRealm
returns Realm instance where the You must not call Realm.close() against returned instance. Parameters
Returns Realm instance where the Throws
|
Returns Realm instance where this RealmObject belongs. You must not call Realm.close() against returned instance. Returns Realm instance where this object belongs to or Throws
|
isFrozen
Returns whether or not this RealmObject is frozen. Returns
|
isLoaded
Checks if the query used to find this RealmObject has completed.Async methods like RealmQuery.findFirstAsync() return an RealmObject that represents the future result of the RealmQuery . It can be considered similar to a java.util.concurrent.Future in this regard. Once "Null" objects represents
Synchronous RealmObjects are by definition blocking hence this method will always return Parameters
Returns
|
public final boolean isLoaded () | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Checks if the query used to find this RealmObject has completed.Async methods like RealmQuery.findFirstAsync() return a RealmObject that represents the future result of the RealmQuery . It can be considered similar to a java.util.concurrent.Future in this regard. Once "Null" objects represents
Synchronous RealmObjects are by definition blocking hence this method will always return Returns
|
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 using addChangeListener(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 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 using Realm.copyFromRealm(RealmModel) . Returns
|
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 using addChangeListener(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 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 using Realm.copyFromRealm(RealmModel) . Returns
|
isValid
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the io.realm.Realm been closed. It will always return Parameters
Returns
|
public final boolean isValid () | |||
---|---|---|---|
Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the io.realm.Realm been closed. It will always return Note that this can be used to check the validity of certain conditions such as being
Returns
|
load
Makes an asynchronous query blocking. This will also trigger any registered listeners.Note: This will return Parameters
Returns
|
public final boolean load () |
---|
Makes an asynchronous query blocking. This will also trigger any registered listeners.Note: This will return Returns
|
removeAllChangeListeners
Removes all registered listeners from the given RealmObject. Parameters
Throws
|
public final void removeAllChangeListeners () |
---|
Removes all registered listeners. |
removeChangeListener
Removes a previously registered listener on the given RealmObject. Parameters
Throws
|
Removes a previously registered listener on the given RealmObject. Parameters
Throws
|
Removes a previously registered listener. Parameters
Throws
|
Removes a previously registered listener. Parameters
Throws
|