Last active
November 30, 2017 04:48
-
-
Save jazzedge/2811bdfed8158d8ffcc46d5601427bcf to your computer and use it in GitHub Desktop.
Use reference fields in your schema to represent relationships between model objects; for example, to represent hierarchical data or indicate ownership.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/CreatingaSchemabySavingRecords/CreatingaSchemabySavingRecords.html | |
| See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingReferences/AddingReferences.html | |
| You can use reference field types to represent both one-to-one and one-to-many relationships between | |
| your model objects. In your code, a reference field is a CKReference object that encapsulates the | |
| record ID for a target record and is added to the source record. To represent a one-to-one relationship | |
| in your schema, add a reference field to the source record type. | |
| To represent a one-to-many relationship between your model objects, it is more efficient if the reference | |
| is from the child record to the parent record—that is, add a reference field to the child record. | |
| The child record is the source, and the parent record is the target in this schema. | |
| For example, a master-detail user interface displays a collection of artwork titles in the | |
| master interface and properties of the selected artwork in the detail interface. | |
| In the code, the underlying object model consists of an Artwork and Artist class where Artwork has a | |
| to-one relationship to Artist. | |
| In the schema, there’s a one-to-one mapping between the objects in this object model and the | |
| record types Artwork and Artist. The artist field of the Artwork record type is a Reference | |
| type with a reference to an Artist record. The image field is an Asset type containing a URL, | |
| and the location field is a Location type with longitude and latitude properties. | |
| All other fields in Artwork and Artist are simple String and Date types. | |
| Conversely, there’s a one-to-many relationship between the Artist and Artwork models compared to | |
| the inverse one-to-one relationship from the Artwork to Artist model. | |
| To represent these relationships in the schema, add a reference field to the corresponding Artwork | |
| record type called artist. This reference field will contain the record ID of an Artist record. | |
| Similarly, to represent the one-to-many relationship from Artist to Collection in the object model, | |
| add a reference field to the Collection record. After you fetch these records, you create the | |
| appropriate one-to-one and one-to-many relationships between the model objects. | |
| 01. Create Reference Fields | |
| During development, save records containing reference fields to generate the schema. | |
| Represent a one-to-one relationship in your schema by adding a CKReference object to the source record. | |
| To create a reference from one record to another | |
| Create or get the record ID for the target record. | |
| var artistRecordID = CKRecordID(recordName: "Mei Chen") | |
| Create a reference object by passing the target’s record ID as a parameter. | |
| var artistReference = CKReference(recordID: artistRecordID, action: .none) | |
| Add the reference object to the source record. | |
| var artworkRecord: CKRecord? | |
| ... | |
| artworkRecord["artist"] = artistReference | |
| Save the source record to create the record type, as described in Initializating the Container. | |
| If you want to save multiple records containing references between them, save all the records | |
| in one operation, as described in Batch Operations to Save and Fetch Multiple Records. | |
| CloudKit will ensure that target records are saved before source records. | |
| 02. Fetch Records with Reference Fields | |
| Don’t use records (CKRecord objects) as your model objects in the Model-View-Controller design pattern. | |
| Instead, create separate model objects from fetched records, especially when the fetched records contain | |
| references. Your app is responsible for interpreting the references between records and creating the | |
| appropriate relationships between model objects. Target records are not automatically fetched when the | |
| source record is fetched. Your app is responsible for fetching target records as needed. There are | |
| different ways to fetch target and source records depending on the type of relationship the reference | |
| represents. | |
| If possible, batch fetches to resolve relationships between model objects, as described in Batch | |
| Operations to Save and Fetch Multiple Records. | |
| For more details, see: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingReferences/AddingReferences.html | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment