Docs Menu

Docs HomeDevelop ApplicationsAtlas Device SDKs

Model Relationships - Swift SDK

On this page

  • Declare Relationship Properties
  • Define a To-One Relationship Property
  • Define a To-Many Relationship Property
  • Define an Inverse Relationship Property
  • Define an Embedded Object Property

Tip

See also:

Alternatively, you can define your relationships in your App Services app.

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.

Setting a relationship field to null removes the connection between objects. Realm does not delete the referenced object, though, unless it is an embedded object.

Important

To-one relationships must be optional

When you declare a to-one relationship in your object model, it must be an optional property. If you try to make a to-one relationship required, Realm throws an exception at runtime.

Tip

See also:

For more information about to-one relationships, see: To-One Relationship.

If your app uses Device Sync, see the Model Data with Device Sync page for information on how the to-one relationship in Swift object models translates to Atlas documents.

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.

Tip

See also:

For more information about to-many relationships, see: To-Many Relationship.

If your app uses Device Sync, see the Model Data with Device Sync page for information on how the to-many relationship in Swift object models translates to Atlas documents.

An inverse relationship property is an automatic backlink relationship. Realm 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.

Tip

See also:

For more information about inverse relationships, see: Inverse Relationship.

If your app uses Device Sync, see the Model Data with Device Sync page for information on how the inverse relationship in Swift object models translates to Atlas documents.

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.

Tip

See also:

If your app uses Device Sync, see the Model Data with Device Sync page for information on how embedded objects in Swift object models translate to Atlas documents.

← Define a Realm Object Model - Swift SDK