Docs Menu

Class RealmObjectSchema

On this page

java.lang.Object
io.realm.RealmObjectSchema

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 RealmObjectSchema is retrieved from an immutable RealmSchema , this RealmObjectSchema will be immutable as well.

Tip
See also:
Modifier and Type
Class and Description
public static interface
Modifier and Type
Method and Description
public abstract RealmObjectSchema

Adds a new simple field to the RealmObject class.

public abstract RealmObjectSchema
String fieldName
)

Adds an index to a given field.

public abstract RealmObjectSchema
String fieldName
)

Adds a primary key to a given field.

public abstract RealmObjectSchema
String fieldName,
java.lang.Class<?> primitiveType
)

Adds a new field that references a RealmList with primitive values.

public abstract RealmObjectSchema
String fieldName,
RealmObjectSchema objectSchema
)

Adds a new field that contains a RealmList with references to other Realm model classes.

public abstract RealmObjectSchema

Adds a new field that references another RealmObject .

public String

Returns the name of the RealmObject class being represented by this schema.

public Set

Returns all fields in this class.

String fieldName
)

Returns the type used by the underlying storage engine to represent this field.

public String

Returns the name of the primary key field.

protected final SchemaConnector
public boolean
String fieldName
)

Tests if the class has field defined with the given name.

public boolean
String fieldName
)

Checks if a given field has an index defined.

public boolean

Checks if the class has a primary key defined.

public boolean

Returns true if objects of this type are considered "embedded".

public boolean
String fieldName
)

Checks if a given field is nullable i.e., it is allowed to contain null values.

public boolean
String fieldName
)

Checks if a given field is the primary key field.

public boolean
String fieldName
)

Checks if a given field is required i.e., it is not allowed to contain null values.

public abstract RealmObjectSchema
String fieldName
)

Removes a field from the class.

public abstract RealmObjectSchema
String fieldName
)

Removes an index from a given field.

public abstract RealmObjectSchema

Removes the primary key from this class.

public abstract RealmObjectSchema
String currentFieldName,
String newFieldName
)

Renames a field from one name to another.

public abstract RealmObjectSchema
String className
)

Sets a new name for this RealmObject class.

public void
boolean embedded
)

Converts the class to be embedded or not.

public abstract RealmObjectSchema
String fieldName,
boolean nullable
)

Sets a field to be nullable i.e., it should be able to hold null values.

public abstract RealmObjectSchema
String fieldName,
boolean required
)

Sets a field to be required i.e., it is not allowed to hold null values.

public abstract RealmObjectSchema

Runs a transformation function on each RealmObject instance of the current class.

  • Methods inherited from class java.lang.Object : getClass , hashCode , equals , clone , toString , notify , notifyAll , wait , wait , wait , finalize
public abstract RealmObjectSchema addField (
String fieldName,
)

Adds a new simple field to the RealmObject class. The type must be one supported by Realm. See RealmObject for the list of supported types. If the field should allow null values use the boxed type instead e.g., Integer.class instead of int.class .

To add fields that reference other RealmObjects or RealmLists use addRealmObjectField(String, RealmObjectSchema) or addRealmListField(String, RealmObjectSchema) instead.

Parameters

  • fieldName - name of the field to add.
  • fieldType - type of field to add. See RealmObject for the full list.
  • attributes - set of attributes for this field.

Returns

the updated schema.

Throws

public abstract RealmObjectSchema addIndex (
String fieldName
)

Adds an index to a given field. This is the equivalent of adding the io.realm.annotations.Index annotation on the field.

Parameters

  • fieldName - field to add index to.

Returns

the updated schema.

Throws

String fieldName
)

Adds a primary key to a given field. This is the same as adding the io.realm.annotations.PrimaryKey annotation on the field. Further, this implicitly adds io.realm.annotations.Index annotation to the field as well.

Parameters

  • fieldName - field to set as primary key.

Returns

the updated schema.

Throws

String fieldName,
java.lang.Class<?> primitiveType
)

Adds a new field that references a RealmList with primitive values. See RealmObject for the list of supported types.

Nullability of elements are defined by using the correct class e.g., Integer.class instead of int.class . Alternatively setRequired(String, boolean) can be used.

Example:

// Defines the list of Strings as being non null.
RealmObjectSchema schema = schema.create("Person")
.addRealmListField("children", String.class)
.setRequired("children", true)

If the list contains references to other Realm classes, use 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

String fieldName,
RealmObjectSchema objectSchema
)

Adds a new field that contains a RealmList with 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

String fieldName,
RealmObjectSchema objectSchema
)

Adds a new field that references another RealmObject .

Parameters

  • fieldName - name of the field to add.
  • objectSchema - schema for the Realm type being referenced.

Returns

the updated schema.

Throws

Returns the name of the RealmObject class being represented by this schema.

  • When using a normal Realm this name is the same as the RealmObject class.
  • When using a DynamicRealm this is the name used in all API methods requiring a class name.

Returns

the name of the RealmObject class represented by this schema.

Throws

public Set getFieldNames ()

Returns all fields in this class.

Returns

a list of all the fields in this class.

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.

Returns the name of the primary key field.

Returns

the name of the primary key field.

Throws

protected final SchemaConnector getSchemaConnector ()

public boolean hasField (
String fieldName
)

Tests if the class has field defined with the given name.

Parameters

  • fieldName - field name to test.

Returns

true if the field exists, false otherwise.

public boolean hasIndex (
String fieldName
)

Checks if a given field has an index defined.

Parameters

  • fieldName - existing field name to check.

Returns

true if field is indexed, false otherwise.

Throws

public boolean hasPrimaryKey ()

Checks if the class has a primary key defined.

Returns

true if a primary key is defined, false otherwise.

public boolean isEmbedded ()

Returns true if objects of this type are considered "embedded". See RealmClass.embedded() for further details.

Returns

true if objects of this type are embedded. false if not.

public boolean isNullable (
String fieldName
)

Checks if a given field is nullable i.e., it is allowed to contain null values.

Parameters

  • fieldName - field to check.

Returns

true if it is required, false otherwise.

Throws

public boolean isPrimaryKey (
String fieldName
)

Checks if a given field is the primary key field.

Parameters

  • fieldName - field to check.

Returns

true if it is the primary key field, false otherwise.

Throws

public boolean isRequired (
String fieldName
)

Checks if a given field is required i.e., it is not allowed to contain null values.

Parameters

  • fieldName - field to check.

Returns

true if it is required, false otherwise.

Throws

public abstract RealmObjectSchema removeField (
String fieldName
)

Removes a field from the class.

Parameters

  • fieldName - field name to remove.

Returns

the updated schema.

Throws

public abstract RealmObjectSchema removeIndex (
String fieldName
)

Removes an index from a given field. This is the same as removing the @Index annotation on the field.

Parameters

  • fieldName - field to remove index from.

Returns

the updated schema.

Throws

Removes the primary key from this class. This is the same as removing the io.realm.annotations.PrimaryKey annotation from the class. Further, this implicitly removes io.realm.annotations.Index annotation from the field as well.

Returns

the updated schema.

Throws

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

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

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 IllegalStateException will be thrown.

Throws

  • IllegalStateException - if the class could not be converted because it broke some of the Embedded Objects invariants.
public abstract RealmObjectSchema setNullable (
String fieldName,
boolean nullable
)

Sets a field to be nullable i.e., it should be able to hold null values. This is equivalent to switching between primitive types and their boxed variant e.g., int to Integer .

If the type of designated field is a list of values (not RealmObject s , 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 - true if field should be nullable, false otherwise.

Returns

the updated schema.

Throws

public abstract RealmObjectSchema setRequired (
String fieldName,
boolean required
)

Sets a field to be required i.e., it is not allowed to hold null values. This is equivalent to switching between boxed types and their primitive variant e.g., Integer to int .

If the type of designated field is a list of values (not RealmObject s , 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 - true if field should be required, false otherwise.

Returns

the updated schema.

Throws

Tip
See also:

Runs a transformation function on each RealmObject instance of the current class. The object will be represented as a DynamicRealmObject .

There is no guarantees in which order the objects are returned.

Parameters

  • function - transformation function.

Returns

this schema.

Throws

←  Interface RealmObjectChangeListenerInterface RealmObjectSchema.Function →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.