Managing Relationships Between Entities¶
There are multiple ways to create relationships based on OData specification.
In general, you can do any of the following:
Create relationship between existing entities.
Create a new entity and establish relationship with an existing one.
Create new entities and establish relationships between them.
Create Relationship Between Existing Entities¶
This approach is used when entities on both ends of the relationship can exist independently. For example, an event can exist without any features and a feature can exist without any associated event.
To create a relationship between existing entities, use the
createLink() method from
DataService or a subclass. And use the
deleteLink() method to delete a relationship.
/// Locate the event to be linked let event = ... /// Locate the feature to be linked with the event let feature = ... /// Link them together try eventService.createLink(from: event, property: Event.features, to: feature) /// Delete the relationship between the feature and the event try eventService.deleteLink(from: event, property: Event.features, to: feature)
Since the cardinality of an event-feature relationship is many-to-many, a relationship between an event and a feature can be created or deleted, but cannot be updated.
In this example , there is only one global theme per event.
/// Locate the global theme for the event let theme = ... /// Establish a relationship between the event and the global theme try eventService.createLink(from: event, property: Event.theme, to: theme) /// Use another theme let anotherTheme = ... /// Link the event to the other theme try eventService.updateLink(from: event, property: Event.theme, to: anotherTheme)
Create a New Entity and Associate It with an Existing Entity¶
There are two approaches to create a new entity and associate it with an existing entity. Each approach generates a different OData request. However, some OData services may only accept one of the two approaches.
Create with Bind This approach creates the new entity against its own entity set. The new entity is bound to an existing entity with which the relationship is to be established.
/// Declare a new session let session = Session(withDefaults: false) /// Set session properties ... /// Locate the event to associate with let event = ... /// Bind the new session to the existing event session.bindEntity(event, to: Session.event) /// Create the new session try eventService.createEntity(session)
Create Related Entity This approach creates the new entity against navigation property of an existing entity with which the relationship is to be established.
/// Declare a new session let session = Session(withDefaults: false) /// Set session properties ... /// Locate the event to associate with let event = ... /// Create new session and associate it with the specified event try eventService.createRelatedEntity(session, in: event, property: Event.sessions)
Deep insert is an approach to create an entity with inline definitions of related entities. Support of this varies between OData services. Offline OData also has some limitations with deep inert. See Limitations with Deep Inserts.
/// Declare a new event let newEvent = Event(withDefaults: false) /// Set event properties ... /// Declare a new session let newSession = Session(withDefaults: false) /// Set session properties ... /// Assign the new event to the new session newSession.event = newEvent /// Create the new session with the new event in-line try eventService.createEntity(newSession)
Note that due to an Offline OData limitation, you cannot create a new event with a number of new sessions using deep insert.
Batch and Change Set¶
This approach takes advantage of OData batch and change set. A batch can contain multiple queries and change sets. A change set can contain multiple requests that make changes to data. Creating multiple entities and establishing relationships between them can be done using a feature of OData change set whereby later requests can reference entities created by former requests. As a result, in the same change set, you can create a parent entity first, and then create its child entities by referencing it.
let changeSet = ChangeSet() let batch = RequestBatch() /// Declare a new event let newEvent = Event(withDefaults: false) /// Set properties for the new event ... /// Declare a new session let newSession = Session(withDefaults: false) /// Set properties for the new session ... /// Create the new event in change set changeSet.createEntity(newEvent) /// Create the new session in change set. This indicates that the session is dependent upon creation of the event changeSet.createRelatedEntity(newSession, in: newEvent, property: Event.sessions) /// Add change set to batch batch.addChanges(changeSet) /// Execute the batch request try eventService.processBatch(batch)