Navigation

Define a Realm Object Schema - iOS SDK

On this page

Note

Class names are limited to a maximum of 57 UTF-8 characters.

Tip

For reference on which types Realm Database supports for use as properties, see Supported Property Types - iOS SDK.

Note

Property names are limited to a maximum of 63 UTF-8 characters.

Primary keys enforce uniqueness among objects in a realm.

You can efficiently find, update, and upsert objects with a primary key.

As long as an object is managed by a realm, that object's primary key value is immutable.

Indexes make queries using equality and IN operators faster in exchange for slightly slower writes. Indexes take up more space in the realm file. It’s best to only add indexes when optimizing the read performance for specific situations.

Realm supports indexing for string, integer, boolean, Date, UUID, ObjectId, and AnyRealmValue properties.

New in version 10.8.0: UUID and AnyRealmValue types

Ignored properties behave exactly like normal properties. They can’t be used in queries and won’t trigger Realm notifications. You can still observe them using KVO.

Tip

Realm automatically ignores read-only properties.

A to-one relationship maps one property to a single instance of another object type. For example, you can model a person having at most one companion dog as a to-one relationship.

A to-many relationship maps one property to zero or more instances of another object type. For example, you can model a person having any number of companion dogs as a to-many relationship.

An inverse relationship property is an automatic backlink relationship. Realm Database automatically updates implicit relationships whenever an object is added or removed in a corresponding to-many list or to-one relationship property. You cannot manually set the value of an inverse relationship property.

An embedded object exists as nested data inside of a single, specific parent object. It inherits the lifecycle of its parent object and cannot exist as an independent Realm object. Realm automatically deletes embedded objects if their parent object is deleted or when overwritten by a new embedded object instance.

Note
Realm Uses Cascading Deletes for Embedded Objects

When you delete a Realm object, any embedded objects referenced by that object are deleted with it. If you want the referenced objects to persist after the deletion of the parent object, your type should not be an embedded object at all. Use a regular Realm object with a to-one relationship instead.

Give Feedback

On this page