Class DynamicRealmObject
- java.lang.Object
-
- io.realm.RealmObject
-
- io.realm.DynamicRealmObject
-
- All Implemented Interfaces:
io.realm.internal.ManageableObject
,io.realm.internal.RealmObjectProxy
,RealmModel
public class DynamicRealmObject extends RealmObject implements io.realm.internal.RealmObjectProxy
Class that wraps a normal RealmObject in order to allow dynamic access instead of a typed interface. Using a DynamicRealmObject is slower than using the regular RealmObject class.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.realm.internal.RealmObjectProxy
io.realm.internal.RealmObjectProxy.CacheData<E extends RealmModel>
-
-
Constructor Summary
Constructors Constructor Description DynamicRealmObject(RealmModel obj)
Creates a dynamic Realm object based on an existing object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
<E> E
get(String fieldName)
Returns the value for the given field.byte[]
getBlob(String fieldName)
Returns thebyte[]
value for a given field.boolean
getBoolean(String fieldName)
Returns theboolean
value for a given field.byte
getByte(String fieldName)
Returns thebyte
value for a given field.Date
getDate(String fieldName)
Returns theDate
value for a given field.Decimal128
getDecimal128(String fieldName)
Returns theDecimal128
value for a given field.RealmDictionary<DynamicRealmObject>
getDictionary(String fieldName)
Returns theRealmDictionary
ofDynamicRealmObject
s being linked from the given field.<E> RealmDictionary<E>
getDictionary(String fieldName, Class<E> primitiveType)
Returns theRealmDictionary
containing only primitive values.double
getDouble(String fieldName)
Returns thedouble
value for a given field.DynamicRealm
getDynamicRealm()
ReturnsDynamicRealm
instance where thisDynamicRealmObject
belongs.String[]
getFieldNames()
Returns the list of field names on this object.RealmFieldType
getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.float
getFloat(String fieldName)
Returns thefloat
value for a given field.int
getInt(String fieldName)
Returns theint
value for a given field.RealmList<DynamicRealmObject>
getList(String fieldName)
Returns theRealmList
ofDynamicRealmObject
s being linked from the given field.<E> RealmList<E>
getList(String fieldName, Class<E> primitiveType)
Returns theRealmList
containing only primitive values.long
getLong(String fieldName)
Returns thelong
value for a given field.DynamicRealmObject
getObject(String fieldName)
Returns the object being linked to from this field.ObjectId
getObjectId(String fieldName)
Returns theObjectId
value for a given field.RealmAny
getRealmAny(String fieldName)
Returns theRealmAny
value for a given field.RealmSet<DynamicRealmObject>
getRealmSet(String fieldName)
Returns theRealmSet
ofDynamicRealmObject
s being linked from the given field.<E> RealmSet<E>
getRealmSet(String fieldName, Class<E> primitiveType)
Returns theRealmSet
containing only primitive values.short
getShort(String fieldName)
Returns theshort
value for a given field.String
getString(String fieldName)
Returns theString
value for a given field.String
getType()
Returns the type of object.UUID
getUUID(String fieldName)
Returns theUUID
value for a given field.boolean
hasField(String fieldName)
Checks whether an object has the given field or not.int
hashCode()
Returns a hash code value for theDynamicRealmObject
object.boolean
isNull(String fieldName)
Checks if the value of a given field isnull
.RealmResults<DynamicRealmObject>
linkingObjects(String srcClassName, String srcFieldName)
ReturnsRealmResults
containing allsrcClassName
class objects that have a relationship to this object fromsrcFieldName
field.void
realm$injectObjectContext()
ProxyState
realmGet$proxyState()
void
set(String fieldName, Object value)
Sets the value for the given field.void
setBlob(String fieldName, byte[] value)
Sets the binary value of the given field.void
setBoolean(String fieldName, boolean value)
Sets theboolean
value of the given field.void
setByte(String fieldName, byte value)
Sets thebyte
value of the given field.void
setDate(String fieldName, Date value)
Sets theDate
value of the given field.void
setDecimal128(String fieldName, Decimal128 value)
Sets theDecimal128
value of the given field.<E> void
setDictionary(String fieldName, RealmDictionary<E> dictionary)
Sets the reference to aRealmDictionary
on the given field.void
setDouble(String fieldName, double value)
Sets thedouble
value of the given field.void
setFloat(String fieldName, float value)
Sets thefloat
value of the given field.void
setInt(String fieldName, int value)
Sets theint
value of the given field.<E> void
setList(String fieldName, RealmList<E> list)
Sets the reference to aRealmList
on the given field.void
setLong(String fieldName, long value)
Sets thelong
value of the given field.void
setNull(String fieldName)
Sets the value tonull
for the given field.void
setObject(String fieldName, DynamicRealmObject value)
Sets a reference to another object on the given field.void
setObjectId(String fieldName, ObjectId value)
Sets theObjectId
value of the given field.void
setRealmAny(String fieldName, RealmAny value)
Sets theRealmAny
value of the given field.<E> void
setRealmSet(String fieldName, RealmSet<E> set)
Sets the reference to aRealmSet
on the given field.void
setShort(String fieldName, short value)
Sets theshort
value of the given field.void
setString(String fieldName, String value)
Sets theString
value of the given field.void
setUUID(String fieldName, UUID value)
Sets theUUID
value of the given field.String
toString()
-
Methods inherited from class io.realm.RealmObject
addChangeListener, addChangeListener, addChangeListener, addChangeListener, asChangesetObservable, asChangesetObservable, asFlowable, asFlowable, deleteFromRealm, deleteFromRealm, freeze, freeze, getRealm, getRealm, isFrozen, isFrozen, isLoaded, isLoaded, isManaged, isManaged, isValid, isValid, load, load, removeAllChangeListeners, removeAllChangeListeners, removeChangeListener, removeChangeListener, removeChangeListener, removeChangeListener
-
-
-
-
Constructor Detail
-
DynamicRealmObject
public DynamicRealmObject(RealmModel obj)
Creates a dynamic Realm object based on an existing object.- Parameters:
obj
- the Realm object to convert to a dynamic object. Only objects managed byRealm
can be used.- Throws:
IllegalArgumentException
- if object isn't managed by Realm or is aDynamicRealmObject
already.
-
-
Method Detail
-
get
public <E> E get(String fieldName)
Returns the value for the given field.- Parameters:
fieldName
- name of the field.- Returns:
- the field value.
- Throws:
ClassCastException
- if the field doesn't contain a field of the defined return type.
-
getBoolean
public boolean getBoolean(String fieldName)
Returns theboolean
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the boolean value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain booleans.RealmException
- if the return value would benull
.
-
getInt
public int getInt(String fieldName)
Returns theint
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the int value. Integer values exceeding
Integer.MAX_VALUE
will wrap. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain integers.RealmException
- if the return value would benull
.
-
getShort
public short getShort(String fieldName)
Returns theshort
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the short value. Integer values exceeding
Short.MAX_VALUE
will wrap. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain integers.RealmException
- if the return value would benull
.
-
getLong
public long getLong(String fieldName)
Returns thelong
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the long value. Integer values exceeding
Long.MAX_VALUE
will wrap. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain integers.RealmException
- if the return value would benull
.
-
getByte
public byte getByte(String fieldName)
Returns thebyte
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the byte value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain integers.RealmException
- if the return value would benull
.
-
getFloat
public float getFloat(String fieldName)
Returns thefloat
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the float value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain floats.RealmException
- if the return value would benull
.
-
getDouble
public double getDouble(String fieldName)
Returns thedouble
value for a given field.If the field is nullable, use
isNull(String)
to check fornull
instead of using this method.- Parameters:
fieldName
- the name of the field.- Returns:
- the double value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain doubles.RealmException
- if the return value would benull
.
-
getBlob
public byte[] getBlob(String fieldName)
Returns thebyte[]
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the byte[] value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain binary data.
-
getString
public String getString(String fieldName)
Returns theString
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the String value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain Strings.
-
getDate
public Date getDate(String fieldName)
Returns theDate
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the Date value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain Dates.
-
getDecimal128
public Decimal128 getDecimal128(String fieldName)
Returns theDecimal128
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the Decimal128 value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain Decimal128.
-
getObjectId
public ObjectId getObjectId(String fieldName)
Returns theObjectId
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the ObjectId value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain ObjectId.
-
getRealmAny
public RealmAny getRealmAny(String fieldName)
Returns theRealmAny
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the RealmAny value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain RealmAny.
-
getUUID
public UUID getUUID(String fieldName)
Returns theUUID
value for a given field.- Parameters:
fieldName
- the name of the field.- Returns:
- the UUID value.
- Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain UUID.
-
getObject
@Nullable public DynamicRealmObject getObject(String fieldName)
Returns the object being linked to from this field.- Parameters:
fieldName
- the name of the field.- Returns:
- the
DynamicRealmObject
representation of the linked object ornull
if no object is linked. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain links to other objects.
-
getList
public RealmList<DynamicRealmObject> getList(String fieldName)
Returns theRealmList
ofDynamicRealmObject
s being linked from the given field.If the list contains primitive types, use
getList(String, Class)
instead.- Parameters:
fieldName
- the name of the field.- Returns:
- the
RealmList
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a list of objects.
-
getList
public <E> RealmList<E> getList(String fieldName, Class<E> primitiveType)
Returns theRealmList
containing only primitive values.If the list contains references to other Realm objects, use
getList(String)
instead.- Parameters:
fieldName
- the name of the field.primitiveType
- the type of elements in the list. Only primitive types are supported.- Returns:
- the
RealmList
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a list of primitive objects.
-
getDictionary
public RealmDictionary<DynamicRealmObject> getDictionary(String fieldName)
Returns theRealmDictionary
ofDynamicRealmObject
s being linked from the given field.If the dictionary contains primitive types, use
getDictionary(String, Class)
instead.- Parameters:
fieldName
- the name of the field.- Returns:
- the
RealmDictionary
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a dictionary of objects.
-
getDictionary
public <E> RealmDictionary<E> getDictionary(String fieldName, Class<E> primitiveType)
Returns theRealmDictionary
containing only primitive values.If the dictionary contains references to other Realm objects, use
getDictionary(String)
instead.- Parameters:
fieldName
- the name of the field.primitiveType
- the type of elements in the dictionary. Only primitive types are supported.- Returns:
- the
RealmDictionary
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a dictionary of primitive objects.
-
getRealmSet
public RealmSet<DynamicRealmObject> getRealmSet(String fieldName)
Returns theRealmSet
ofDynamicRealmObject
s being linked from the given field.If the set contains primitive types, use
getRealmSet(String, Class)
instead.- Parameters:
fieldName
- the name of the field.- Returns:
- the
RealmSet
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a set of objects.
-
getRealmSet
public <E> RealmSet<E> getRealmSet(String fieldName, Class<E> primitiveType)
Returns theRealmSet
containing only primitive values.If the set contains references to other Realm objects, use
getRealmSet(String)
instead.- Parameters:
fieldName
- the name of the field.primitiveType
- the type of elements in the set. Only primitive types are supported.- Returns:
- the
RealmSet
data for this field. - Throws:
IllegalArgumentException
- if field name doesn't exist or it doesn't contain a set of primitive objects.
-
isNull
public boolean isNull(String fieldName)
Checks if the value of a given field isnull
.- Parameters:
fieldName
- the name of the field.- Returns:
true
if field value is null,false
otherwise.- Throws:
IllegalArgumentException
- if field name doesn't exist.
-
hasField
public boolean hasField(String fieldName)
Checks whether an object has the given field or not.- Parameters:
fieldName
- field name to check.- Returns:
true
if the object has a field with the given name,false
otherwise.
-
getFieldNames
public String[] getFieldNames()
Returns the list of field names on this object.- Returns:
- list of field names on this objects or the empty list if the object doesn't have any fields.
-
set
public void set(String fieldName, Object value)
Sets the value for the given field. This method will automatically try to convert numbers and booleans that are given asString
to their appropriate type. For example"10"
will be converted to10
if the field type isint
.Using the typed setters will be faster than using this method.
- Throws:
IllegalArgumentException
- if field name doesn't exist or if the input value cannot be converted to the appropriate input type.NumberFormatException
- if a String based number cannot be converted properly.RealmException
- if the field is aPrimaryKey
field.
-
setBoolean
public void setBoolean(String fieldName, boolean value)
Sets theboolean
value of the given field.- Parameters:
fieldName
- field name to update.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a boolean field.
-
setShort
public void setShort(String fieldName, short value)
Sets theshort
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't an integer field.RealmException
- if the field is aPrimaryKey
field.
-
setInt
public void setInt(String fieldName, int value)
Sets theint
value of the given field.- Parameters:
fieldName
- field name to update.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't an integer field.RealmException
- if the field is aPrimaryKey
field.
-
setLong
public void setLong(String fieldName, long value)
Sets thelong
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't an integer field.RealmException
- if the field is aPrimaryKey
field.
-
setByte
public void setByte(String fieldName, byte value)
Sets thebyte
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't an integer field.RealmException
- if the field is aPrimaryKey
field.
-
setFloat
public void setFloat(String fieldName, float value)
Sets thefloat
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a float field.
-
setDouble
public void setDouble(String fieldName, double value)
Sets thedouble
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a double field.
-
setString
public void setString(String fieldName, @Nullable String value)
Sets theString
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a String field.RealmException
- if the field is aPrimaryKey
field.
-
setBlob
public void setBlob(String fieldName, @Nullable byte[] value)
Sets the binary value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a binary field.
-
setDate
public void setDate(String fieldName, @Nullable Date value)
Sets theDate
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a Date field.
-
setDecimal128
public void setDecimal128(String fieldName, @Nullable Decimal128 value)
Sets theDecimal128
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a Decimal128 field.
-
setObjectId
public void setObjectId(String fieldName, @Nullable ObjectId value)
Sets theObjectId
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a ObjectId field.
-
setRealmAny
public void setRealmAny(String fieldName, @Nullable RealmAny value)
Sets theRealmAny
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a RealmAny field.
-
setUUID
public void setUUID(String fieldName, @Nullable UUID value)
Sets theUUID
value of the given field.- Parameters:
fieldName
- field name.value
- value to insert.- Throws:
IllegalArgumentException
- if field name doesn't exist or field isn't a UUID field.
-
setObject
public void setObject(String fieldName, @Nullable DynamicRealmObject value)
Sets a reference to another object on the given field.- Parameters:
fieldName
- field name.value
- object to link to.- Throws:
IllegalArgumentException
- if field name doesn't exist, it doesn't link to other Realm objects, the type of DynamicRealmObject doesn't match or it belongs to a different Realm.
-
setList
public <E> void setList(String fieldName, RealmList<E> list)
Sets the reference to aRealmList
on the given field.This will copy all the elements in the list into Realm, but any further changes to the list will not be reflected in the Realm. Use
getList(String)
in order to get a reference to the managed list.- Parameters:
fieldName
- field name.list
- list of objects. Must either be primitive types orDynamicRealmObject
s.- Throws:
IllegalArgumentException
- if field name doesn't exist, it is not a list field, the objects in the list doesn't match the expected type or any Realm object in the list belongs to a different Realm.
-
setDictionary
public <E> void setDictionary(String fieldName, RealmDictionary<E> dictionary)
Sets the reference to aRealmDictionary
on the given field.This will copy all the elements in the dictionary into Realm, but any further changes to the dictionary will not be reflected in the Realm. Use
getDictionary(String)
in order to get a reference to the managed dictionary.- Parameters:
fieldName
- field name.dictionary
- dictionary of objects. Must either be primitive types orDynamicRealmObject
s.- Throws:
IllegalArgumentException
- if field name doesn't exist, it is not a dictionary field, the objects in the dictionary doesn't match the expected type or any Realm object in the dictionary belongs to a different Realm.
-
setRealmSet
public <E> void setRealmSet(String fieldName, RealmSet<E> set)
Sets the reference to aRealmSet
on the given field.This will copy all the elements in the set into Realm, but any further changes to the set will not be reflected in the Realm. Use
getRealmSet(String)
in order to get a reference to the managed set.- Parameters:
fieldName
- field name.set
- set of objects. Must either be primitive types orDynamicRealmObject
s.- Throws:
IllegalArgumentException
- if field name doesn't exist, it is not a set field, the objects in the set doesn't match the expected type or any Realm object in the set belongs to a different Realm.
-
setNull
public void setNull(String fieldName)
Sets the value tonull
for the given field.- Parameters:
fieldName
- field name.- Throws:
IllegalArgumentException
- if field name doesn't exist, or the field isn't nullable.RealmException
- if the field is aPrimaryKey
field.
-
getType
public String getType()
Returns the type of object. This will normally correspond to the name of a class that is extendingRealmObject
.- Returns:
- this objects type.
-
getFieldType
public RealmFieldType getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.- Returns:
- the underlying type used by Realm to represent this field.
-
hashCode
public int hashCode()
Returns a hash code value for theDynamicRealmObject
object.By the general contract of
Object.hashCode()
, any two objects for whichequals(java.lang.Object)
returnstrue
must return the same hash code value.Note that a
RealmObject
is a live object, and it might be updated by changes from other threads. This means that a hash code value of the object is not stable, and the value should be neither used as a key in HashMap nor saved in HashSet.- Overrides:
hashCode
in classObject
- Returns:
- a hash code value for the object.
- See Also:
equals(java.lang.Object)
-
linkingObjects
public RealmResults<DynamicRealmObject> linkingObjects(String srcClassName, String srcFieldName)
ReturnsRealmResults
containing allsrcClassName
class objects that have a relationship to this object fromsrcFieldName
field.An entry is added for each reference, e.g. if the same reference is in a list multiple times, the src object will show up here multiple times.
- Parameters:
srcClassName
- name of the class returned objects belong to.srcFieldName
- name of the field in the source class that holds a reference to this object. Field type must be eitherio.realm.RealmFieldType.OBJECT
orio.realm.RealmFieldType.LIST
.- Returns:
- the result.
- Throws:
IllegalArgumentException
- if thesrcClassName
isnull
or does not exist, thesrcFieldName
isnull
or does not exist, type of the source field is not supported.
-
getDynamicRealm
public DynamicRealm getDynamicRealm()
ReturnsDynamicRealm
instance where thisDynamicRealmObject
belongs.You must not call
Closeable.close()
against returned instance.- Returns:
DynamicRealm
instance where this object belongs.- Throws:
IllegalStateException
- if this object was deleted or the correspondingDynamicRealm
was already closed.
-
realm$injectObjectContext
public void realm$injectObjectContext()
- Specified by:
realm$injectObjectContext
in interfaceio.realm.internal.RealmObjectProxy
-
realmGet$proxyState
public ProxyState realmGet$proxyState()
- Specified by:
realmGet$proxyState
in interfaceio.realm.internal.RealmObjectProxy
-
-