Specify how entities relate and how change propagates between them.
- Core Data
Once you define at least two entities as described in Configuring Entities, you can add a relationship between the entities.
A relationship describes how an entity affects another entity. At minimum, a relationship specifies a name, a destination entity, a delete rule, a cardinality type (To One or To Many), settings for whether the relationship should be saved in the store (transient), and whether it is required to have a value when saved (optional). You should also configure every relationship with an inverse relationship.
To add a relationship:
Select the graph editor style to view all of your app’s entities at once.
Control-drag from one entity to another to create a pair of relationships. An arrow appears between the entities to indicate a relationship, and a relationship with placeholder name
newis added to each entity.
After creating a pair of relationships, configure each relationship as indicated in the screenshot and the steps that follow.
Select the table editor style to edit one entity at a time.
Open the Data Model inspector (choose View > Inspectors > Show Data Model Inspector).
Select the source entity from the Entities list, then select the new relationship in the Relationships list. Use the Data Model inspector to configure its name, destination, inverse, delete rule, and cardinality type, and to indicate if it is transient or optional.
Select the destination entity from the Entities list, then select the new relationship in the Relationships list. Use the Data Model inspector to configure its name, destination, inverse, delete rule, and cardinality type, and to indicate if it is transient or optional.
The above example shows a
countries relationship, referring to one or more countries affected by a given earthquake. It has an inverse relationship on the
Country entity called
quakes, referring to any earthquakes affecting that country.
Transient relationships aren’t saved to the persistent store. This makes transient relationships a useful place to temporarily store calculated or derived values. Core Data does track changes to transient property values for undo purposes.
Optional relationships aren’t required to have any instances of their destination type. A required relationship must point to one or more instances of the destination type.
Each relationship points from a source entity (the entity whose relationships you’re editing) to a destination entity. The destination entity is a related type that affects and is affected by the source type.
Setting the same source and destination types creates a reflexive relationship. For example, an
Employeemay manage another
Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship should have an inverse.
When creating relationships in the Graph editor, you add inverse relationships between entities in a single step. When creating relationships in the Table editor, you add inverse relationships to each entity in turn.
- Delete Rule
A relationship’s delete rule specifies how change propagates across relationships when a source instance is deleted.
Select No Action to delete the source object instance, but leave references to it in any destination object instances, which you update manually.
Select Nullify to delete the source object instance, and nullify references to it in any destination object instances.
Select Cascade to delete the source object instance, and with it, all of the destination object instances.
Select Deny to delete the source object only if it doesn’t point to any destination object instances.
- Cardinality Type
Specify a relationship as being To One or To Many, which is known as its cardinality.
Use To One relationships to connect the source with a single instance of the destination type.
Use To Many relationships to connect the source with a mutable set of the destination type, and to optionally specify an arrangement and count:
Arrangement: Select the Ordered checkbox to specify that the relationship has an inherent ordering, and to generate an ordered mutable set.
Count: You can also place upper and lower limits on the number of destination instances. For optional relationships, the number of instances can be zero or within these bounds.
- Index in Spotlight
Adds the field to the Spotlight index for instances created from this entity.
For more information, see Core Spotlight.