swift Swift - CloudKit - 关系中的引用类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit - 关系中的引用类型相关的知识,希望对你有一定的参考价值。

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












以上是关于swift Swift - CloudKit - 关系中的引用类型的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享

swift Swift - CloudKit - 维护本地缓存

swift Swift - CloudKit - 部署架构

swift Swift - CloudKit订阅 - 5