Object Models & Schemas - iOS SDK¶
Realm Database applications model data as objects composed of field-value pairs that each contain one or more supported data types. Realm objects behave like regular Swift or Objective-C objects, but they also include additional features like real-time updating data views and reactive change event handlers.
Objects of the same class share an object schema that defines the fields and relationships of those objects. Every realm has a schema that consists of one or more object schemas describing the different forms of object that can be stored in that realm.
To learn how to define a Realm object in Swift or Objective-C, see Define a Realm Object Schema - iOS SDK.
You can identify a property as the primary key of your class. Primary keys are subject to the following limitations:
- You can define only one primary key per object model.
- Primary key values must be unique across all instances of an object in a realm. Realm Database throws an error if you try to insert a duplicate primary key value.
- Primary key values are immutable. To change the primary key value of an object, you must delete the original object and insert a new object with a different primary key value.
- Embedded objects cannot define a a primary key.
To learn how to define a primary key for your Realm object, see Specify a Primary Key.
You can create an index on a given property of your model. Indexes speed up some queries, but have a negative impact on insert and update operation speed. Indexes also consume additional space on disk to store the actual index information.
To learn how to add an index to your Realm object, see Index a Property.
Realm Database supports to-one, to-many, and inverse relationships. You can specify a relationship by adding a reference or list of references to the related Realm object as a property of your Realm object.
Inverse relationship properties contain the set of Realm objects that point to that object instance through a to-one or to-many relationship. Inverse relationships automatically update themselves with corresponding backlinks. You can find the same set of Realm objects with a manual query, but the inverse relationship field reduces boilerplate query code and capacity for error.
To learn how to add relationships to your Realm object, see Declare Relationship Properties.
- Realm objects are of a type defined as you would any other class.
- The Realm object's schema defines the fields of the object and which fields are optional, which fields have a default value, and which fields are indexed.
- You can define to-one, to-many, and inverse relationships in your schema. Inverse relationships automatically form backlinks.