我有两个简单的记录类型:Artwork
和Artist
。作为一个孩子,Artwork
可能属于一个 Artist
,但是Artist
可能会有多个艺术作品。
以下是与提及的记录类型相关的屏幕截图。
我可以通过以下方式为artist
设置artwork
:
let artistID = CKRecordID(recordName: "F297D690-888B-4B55-9FBD-27CAEF4BBD83")
let artworkID = CKRecordID(recordName: self.idTextField.text!)
self.container.publicCloudDatabase.fetchRecordWithID(artworkID, completionHandler: { artwork, error in
if let artwork = artwork {
artwork["artist"] = CKReference(recordID: artistID, action: CKReferenceAction.None)
self.container.publicCloudDatabase.saveRecord(artwork, completionHandler: { artwork, error in
print("done")
})
}
})
但问题是:
如何以反向关系设置关系?换句话说,如何将artworkID
添加到artistID
的参考列表中?
...表示对象模型中从
Artist
到Collection
的一对多关系,将参考字段添加到Collection
记录。
但是怎么做?
答案 0 :(得分:2)
这是我找到的最简单的答案:
let artworkID = CKRecordID(recordName: self.idTextField.text!)
if let artist = artist, var artworks = artist["artworks"] as? [CKReference] {
let reference = CKReference(recordID: artworkID, action: CKReferenceAction.None)
if !artworks.contains(reference) {
artworks.append(reference)
}
artist["artworks"] = artworks
//here you can save your changes or do whatever it needs
}
答案 1 :(得分:0)
问题的关键可以在Apple文档中找到...
https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKReference_class/
允许为一组记录创建循环拥有引用。
要保存包含引用的多个记录,请先保存目标记录,或使用相同的 CKModifyRecordsOperation 对象 将所有记录保存在一个操作中。
在CloudKit中设置引用的代码我确定你见过吗?
let uniqReference = NSUUID().UUIDString
let unqUUID = CKRecordID(recordName: uniqUUID)
let newRecord = CKRecord(recordType: "Blah", recordID: uniqUUID)
它表示您需要使用CKModifyOperation同时保存所有记录。
func saveLeCollection(theGlob:NSURL) {
let container = CKContainer(identifier: "Blah.com")
let publicDB = container.publicCloudDatabase
let uniqUUID = CKRecordID(recordName: uniqReference)
let newRecord = CKRecord(recordType: "Blah", recordID: uniqUUID)
let whistleAsset = CKAsset(fileURL: theImage)
newRecord["theImage"] = whistleAsset
newRecord.setObject(uniqUUID, forKey: "theLink")
var localChanges:[CKRecord] = []
localChanges.append(newRecord)
let saveRecordsOperation = CKModifyRecordsOperation(recordsToSave: localChanges, recordIDsToDelete: records2Erase)
saveRecordsOperation.savePolicy = .ChangedKeys
saveRecordsOperation.perRecordCompletionBlock = { record, error in
if error != nil {
dispatch_async(dispatch_get_main_queue()) {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
}
}
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error != nil {
dispatch_async(dispatch_get_main_queue()) {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
}
} else {
// deal with conflictsay
// set completionHandler of wrapper operation if it's the case
}
}
}
saveRecordsOperation.qualityOfService = .Background
publicDB.addOperation(saveRecordsOperation)
}
如果我将ArtistID存储在我的艺术作品记录中,则每个艺术作品记录中的相同ID我都有多个>一个参考。通过我的参考列表,我有一个>很多参考,我的艺术家记录指向每个艺术作品记录。