Class RealmObjectSchema
- java.lang.Object
-
- io.realm.RealmObjectSchema
-
public abstract class RealmObjectSchema extends Object
Class for interacting with the schema for a given RealmObject class. This makes it possible to inspect, add, delete or change the fields for given class.If this
RealmObjectSchemais retrieved from an immutableRealmSchema, thisRealmObjectSchemawill be immutable as well.- See Also:
RealmMigration
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceRealmObjectSchema.FunctionFunction interface, used when traversing all objects of the current class and apply a function on each.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract RealmObjectSchemaaddField(String fieldName, Class<?> fieldType, FieldAttribute... attributes)Adds a new simple field to the RealmObject class.abstract RealmObjectSchemaaddIndex(String fieldName)Adds an index to a given field.abstract RealmObjectSchemaaddPrimaryKey(String fieldName)Adds a primary key to a given field.abstract RealmObjectSchemaaddRealmDictionaryField(String fieldName, RealmObjectSchema objectSchema)Adds a new field that contains aRealmDictionarywith references to other Realm model classes.abstract RealmObjectSchemaaddRealmDictionaryField(String fieldName, Class<?> primitiveType)Adds a new field that references aRealmDictionarywith primitive values.abstract RealmObjectSchemaaddRealmListField(String fieldName, RealmObjectSchema objectSchema)Adds a new field that contains aRealmListwith references to other Realm model classes.abstract RealmObjectSchemaaddRealmListField(String fieldName, Class<?> primitiveType)Adds a new field that references aRealmListwith primitive values.abstract RealmObjectSchemaaddRealmObjectField(String fieldName, RealmObjectSchema objectSchema)Adds a new field that references anotherRealmObject.abstract RealmObjectSchemaaddRealmSetField(String fieldName, RealmObjectSchema objectSchema)Adds a new field that contains aRealmSetwith references to other Realm model classes.abstract RealmObjectSchemaaddRealmSetField(String fieldName, Class<?> primitiveType)Adds a new field that references aRealmSetwith primitive values.StringgetClassName()Returns the name of the RealmObject class being represented by this schema.Set<String>getFieldNames()Returns all fields in this class.RealmFieldTypegetFieldType(String fieldName)Returns the type used by the underlying storage engine to represent this field.StringgetPrimaryKey()Returns the name of the primary key field.booleanhasField(String fieldName)Tests if the class has field defined with the given name.booleanhasIndex(String fieldName)Checks if a given field has an index defined.booleanhasPrimaryKey()Checks if the class has a primary key defined.booleanisEmbedded()Returnstrueif objects of this type are considered "embedded".booleanisNullable(String fieldName)Checks if a given field is nullable i.e., it is allowed to containnullvalues.booleanisPrimaryKey(String fieldName)Checks if a given field is the primary key field.booleanisRequired(String fieldName)Checks if a given field is required i.e., it is not allowed to containnullvalues.abstract RealmObjectSchemaremoveField(String fieldName)Removes a field from the class.abstract RealmObjectSchemaremoveIndex(String fieldName)Removes an index from a given field.abstract RealmObjectSchemaremovePrimaryKey()Removes the primary key from this class.abstract RealmObjectSchemarenameField(String currentFieldName, String newFieldName)Renames a field from one name to another.abstract RealmObjectSchemasetClassName(String className)Sets a new name for this RealmObject class.voidsetEmbedded(boolean embedded)Converts the class to be embedded or not.voidsetEmbedded(boolean embedded, boolean resolveEmbeddedClassConstraints)Converts the class to be embedded or not, while also providing automatic handling of objects that break some of the constraints for making the class embedded.abstract RealmObjectSchemasetNullable(String fieldName, boolean nullable)Sets a field to be nullable i.e., it should be able to holdnullvalues.abstract RealmObjectSchemasetRequired(String fieldName, boolean required)Sets a field to be required i.e., it is not allowed to holdnullvalues.abstract RealmObjectSchematransform(RealmObjectSchema.Function function)Runs a transformation function on each RealmObject instance of the current class.
-
-
-
Method Detail
-
getClassName
public String getClassName()
Returns the name of the RealmObject class being represented by this schema.- When using a normal
Realmthis name is the same as theRealmObjectclass. - When using a
DynamicRealmthis is the name used in all API methods requiring a class name.
- Returns:
- the name of the RealmObject class represented by this schema.
- Throws:
IllegalStateException- if this schema defintion is no longer part of the Realm.
- When using a normal
-
setClassName
public abstract RealmObjectSchema setClassName(String className)
Sets a new name for this RealmObject class. This is equivalent to renaming it.- Parameters:
className- the new name for this class.- Throws:
IllegalArgumentException- if className isnullor an empty string, or its length exceeds 56 characters.UnsupportedOperationException- if thisRealmObjectSchemais immutable or from a synced Realm.- See Also:
RealmSchema.rename(String, String)
-
addField
public abstract RealmObjectSchema addField(String fieldName, Class<?> fieldType, FieldAttribute... attributes)
Adds a new simple field to the RealmObject class. The type must be one supported by Realm. SeeRealmObjectfor the list of supported types. If the field should allownullvalues use the boxed type instead e.g.,Integer.classinstead ofint.class.To add fields that reference other RealmObjects or RealmLists use
addRealmObjectField(String, RealmObjectSchema)oraddRealmListField(String, RealmObjectSchema)instead.- Parameters:
fieldName- name of the field to add.fieldType- type of field to add. SeeRealmObjectfor the full list.attributes- set of attributes for this field.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the type isn't supported, field name is illegal or a field with that name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable or if adding a a field withFieldAttribute.PRIMARY_KEYattribute to a schema of a synced Realm.
-
addRealmObjectField
public abstract RealmObjectSchema addRealmObjectField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that references anotherRealmObject.- Parameters:
fieldName- name of the field to add.objectSchema- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name is illegal or a field with that name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmListField
public abstract RealmObjectSchema addRealmListField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmListwith references to other Realm model classes.If the list contains primitive types, use
addRealmListField(String, Class)instead.- Parameters:
fieldName- name of the field to add.objectSchema- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal or a field with that name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmListField
public abstract RealmObjectSchema addRealmListField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmListwith primitive values. SeeRealmObjectfor the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.classinstead ofint.class. AlternativelysetRequired(String, boolean)can be used.Example:
If the list contains references to other Realm classes, use// Defines the list of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmListField("children", String.class) .setRequired("children", true)addRealmListField(String, RealmObjectSchema)instead.- Parameters:
fieldName- name of the field to add.primitiveType- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmDictionaryField
public abstract RealmObjectSchema addRealmDictionaryField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmDictionarywith references to other Realm model classes.If the dictionary contains primitive types, use
addRealmDictionaryField(String, Class)instead.- Parameters:
fieldName- name of the field to add.objectSchema- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal or a field with that name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmDictionaryField
public abstract RealmObjectSchema addRealmDictionaryField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmDictionarywith primitive values. SeeRealmObjectfor the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.classinstead ofint.class. AlternativelysetRequired(String, boolean)can be used.Example:
If the list contains references to other Realm classes, use// Defines the dictionary of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmDictionaryField("parentAndChild", String.class) .setRequired("parentAndChild", true)addRealmDictionaryField(String, RealmObjectSchema)instead.- Parameters:
fieldName- name of the field to add.primitiveType- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmSetField
public abstract RealmObjectSchema addRealmSetField(String fieldName, RealmObjectSchema objectSchema)
Adds a new field that contains aRealmSetwith references to other Realm model classes.If the set contains primitive types, use
addRealmSetField(String, Class)instead.- Parameters:
fieldName- name of the field to add.objectSchema- schema for the Realm type being referenced.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal or a field with that name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
addRealmSetField
public abstract RealmObjectSchema addRealmSetField(String fieldName, Class<?> primitiveType)
Adds a new field that references aRealmSetwith primitive values. SeeRealmObjectfor the list of supported types.Nullability of elements are defined by using the correct class e.g.,
Integer.classinstead ofint.class. AlternativelysetRequired(String, boolean)can be used.Example:
If the list contains references to other Realm classes, use// Defines the set of Strings as being non null. RealmObjectSchema schema = schema.create("Person") .addRealmSetField("children", String.class) .setRequired("children", true)addRealmSetField(String, RealmObjectSchema)instead.- Parameters:
fieldName- name of the field to add.primitiveType- simple type of elements in the array.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name is illegal, a field with that name already exists or the element type isn't supported.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
removeField
public abstract RealmObjectSchema removeField(String fieldName)
Removes a field from the class.- Parameters:
fieldName- field name to remove.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name doesn't exist.UnsupportedOperationException- if thisRealmObjectSchemais immutable or for a synced Realm.
-
renameField
public abstract RealmObjectSchema renameField(String currentFieldName, String newFieldName)
Renames a field from one name to another.- Parameters:
currentFieldName- field name to rename.newFieldName- the new field name.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name doesn't exist or if the new field name already exists.UnsupportedOperationException- if thisRealmObjectSchemais immutable or for a synced Realm.
-
hasField
public boolean hasField(String fieldName)
Tests if the class has field defined with the given name.- Parameters:
fieldName- field name to test.- Returns:
trueif the field exists,falseotherwise.
-
addIndex
public abstract RealmObjectSchema addIndex(String fieldName)
Adds an index to a given field. This is the equivalent of adding theIndexannotation on the field.- Parameters:
fieldName- field to add index to.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name doesn't exist, the field cannot be indexed or it already has a index defined.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
hasIndex
public boolean hasIndex(String fieldName)
Checks if a given field has an index defined.- Parameters:
fieldName- existing field name to check.- Returns:
trueif field is indexed,falseotherwise.- Throws:
IllegalArgumentException- if field name doesn't exist.- See Also:
Index
-
removeIndex
public abstract RealmObjectSchema removeIndex(String fieldName)
Removes an index from a given field. This is the same as removing the@Indexannotation on the field.- Parameters:
fieldName- field to remove index from.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name doesn't exist or the field doesn't have an index.UnsupportedOperationException- if thisRealmObjectSchemais immutable or of a synced Realm.
-
addPrimaryKey
public abstract RealmObjectSchema addPrimaryKey(String fieldName)
Adds a primary key to a given field. This is the same as adding thePrimaryKeyannotation on the field. Further, this implicitly addsIndexannotation to the field as well.- Parameters:
fieldName- field to set as primary key.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if field name doesn't exist, the field cannot be a primary key or it already has a primary key defined.UnsupportedOperationException- if thisRealmObjectSchemais immutable or of a synced Realm.
-
removePrimaryKey
public abstract RealmObjectSchema removePrimaryKey()
Removes the primary key from this class. This is the same as removing thePrimaryKeyannotation from the class. Further, this implicitly removesIndexannotation from the field as well.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the class doesn't have a primary key defined.UnsupportedOperationException- if thisRealmObjectSchemais immutable or of a synced Realm.
-
setRequired
public abstract RealmObjectSchema setRequired(String fieldName, boolean required)
Sets a field to be required i.e., it is not allowed to holdnullvalues. This is equivalent to switching between boxed types and their primitive variant e.g.,Integertoint.If the type of designated field is a list of values (not
RealmObjects , specified nullability only affects its elements, not the field itself. Value list itself is always non-nullable.- Parameters:
fieldName- name of field in the class.required-trueif field should be required,falseotherwise.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name doesn't exist, cannot have theRequiredannotation or the field already have been set as required.UnsupportedOperationException- if thisRealmObjectSchemais immutable.- See Also:
Required
-
setNullable
public abstract RealmObjectSchema setNullable(String fieldName, boolean nullable)
Sets a field to be nullable i.e., it should be able to holdnullvalues. This is equivalent to switching between primitive types and their boxed variant e.g.,inttoInteger.If the type of designated field is a list of values (not
RealmObjects , specified nullability only affects its elements, not the field itself. Value list itself is always non-nullable.- Parameters:
fieldName- name of field in the class.nullable-trueif field should be nullable,falseotherwise.- Returns:
- the updated schema.
- Throws:
IllegalArgumentException- if the field name doesn't exist, or cannot be set as nullable.UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
isRequired
public boolean isRequired(String fieldName)
Checks if a given field is required i.e., it is not allowed to containnullvalues.- Parameters:
fieldName- field to check.- Returns:
trueif it is required,falseotherwise.- Throws:
IllegalArgumentException- if field name doesn't exist.- See Also:
setRequired(String, boolean)
-
isNullable
public boolean isNullable(String fieldName)
Checks if a given field is nullable i.e., it is allowed to containnullvalues.- Parameters:
fieldName- field to check.- Returns:
trueif it is required,falseotherwise.- Throws:
IllegalArgumentException- if field name doesn't exist.- See Also:
setNullable(String, boolean)
-
isPrimaryKey
public boolean isPrimaryKey(String fieldName)
Checks if a given field is the primary key field.- Parameters:
fieldName- field to check.- Returns:
trueif it is the primary key field,falseotherwise.- Throws:
IllegalArgumentException- if field name doesn't exist.- See Also:
addPrimaryKey(String)
-
hasPrimaryKey
public boolean hasPrimaryKey()
Checks if the class has a primary key defined.- Returns:
trueif a primary key is defined,falseotherwise.- See Also:
PrimaryKey
-
getPrimaryKey
public String getPrimaryKey()
Returns the name of the primary key field.- Returns:
- the name of the primary key field.
- Throws:
IllegalStateException- if the class doesn't have a primary key defined.
-
getFieldNames
public Set<String> getFieldNames()
Returns all fields in this class.- Returns:
- a list of all the fields in this class.
-
transform
public abstract RealmObjectSchema transform(RealmObjectSchema.Function function)
Runs a transformation function on each RealmObject instance of the current class. The object will be represented as aDynamicRealmObject.There is no guarantees in which order the objects are returned.
- Parameters:
function- transformation function.- Returns:
- this schema.
- Throws:
UnsupportedOperationException- if thisRealmObjectSchemais immutable.
-
getFieldType
public RealmFieldType getFieldType(String fieldName)
Returns the type used by the underlying storage engine to represent this field.- Parameters:
fieldName- name of the target field.- Returns:
- the underlying type used by Realm to represent this field.
-
isEmbedded
public boolean isEmbedded()
Returnstrueif objects of this type are considered "embedded". SeeRealmClass.embedded()for further details.- Returns:
trueif objects of this type are embedded.falseif not.
-
setEmbedded
public void setEmbedded(boolean embedded)
Converts the class to be embedded or not.A class can only be marked as embedded if the following invariants are satisfied:
- The class is not allowed to have a primary key defined.
-
All existing objects of this type, must have one and exactly one parent object
already pointing to it. If 0 or more than 1 object has a reference to an object
about to be marked embedded an
IllegalStateExceptionwill be thrown.
- Parameters:
embedded- If @{code true}, the class type will be turned into an embedded class, and must satisfy the constraints defined above. If @{code false}, the class will be turn into a normal class. An embeded class can always be turned into a non-embedded one.- Throws:
IllegalStateException- if the class could not be converted because it broke some of the Embedded Objects invariants.- See Also:
RealmClass.embedded()
-
setEmbedded
public void setEmbedded(boolean embedded, boolean resolveEmbeddedClassConstraints)Converts the class to be embedded or not, while also providing automatic handling of objects that break some of the constraints for making the class embedded.A class can only be marked as embedded if the following invariants are satisfied:
- The class is not allowed to have a primary key defined.
-
All existing objects of this type, must have one and exactly one parent object
already pointing to it. If 0 or more than 1 object has a reference to an object
about to be marked embedded an
IllegalStateExceptionwill be thrown.
- An object with 0 parents, i.e. no other objects have a reference to it, will be deleted.
- An object with more than 1 parent, i.e. 2 or more objects have a reference to it, will be copied so each copy have exactly one parent.
- Objects with a primary key defined will still throw an IllegalStateException and cannot be converted.
- Parameters:
embedded- If @{code true}, the class type will be turned into an embedded class, and must satisfy the constraints defined above. If @{code false}, the class will be turn into a normal class. An embeded class can always be turned into a non-embedded one.resolveEmbeddedClassConstraints- whether or not to automatically fix broken constraints if @{code embedded} was set to true. See above for a full description of what that entails.- Throws:
IllegalStateException- if the class could not be converted because it broke some of the Embedded Objects invariants and these could not be resolved automatically.- See Also:
RealmClass.embedded()
-
-