创建子记录时使用子记录名称填充 CloudKit 中的父引用(列表)
Populating parent Reference (List) in CloudKit with child recordName when creating a child record
我正在尝试构建 cloudkit 教程 (https://www.raywenderlich.com/4878052-cloudkit-tutorial-getting-started) 中包含的功能之外的其他功能,并且已经达到了我有一个可以查看机构(餐厅)和关于它们的注释 - 我在 CloudKit 仪表板中创建了数据,并在机构的 'notes' 参考(列表)中预填充了注释的记录名称。
我现在正在扩展该应用程序以允许用户添加注释,并且在我能够创建新注释的位置,并填充 Establishment 的记录名称(在子记录中建立父实体)这个:
let noteRecord = CKRecord(recordType: "Note")
noteRecord["text"] = noteText.text //grabbed from a UIAlert
if self.establishment?.name != nil { //If the establishment exists
let reference = CKRecord.Reference(recordID: self.establishment!.id, action: .deleteSelf)
noteRecord["establishment"] = reference as! CKRecordValue
}
//works fine, saves the new note as expected with the right recordName for the establishment
CKContainer.default().privateCloudDatabase.save(noteRecord) { [unowned self] record, error in
DispatchQueue.main.async {
if let error = error {
} else {
}
}
}
现在我遇到的问题是如何获取这个新保存的注释的记录名称并将其附加到机构的参考(列表)中。
原因是应用程序在教程中的构建方式 - 在获取机构的所有注释时 - 它使用机构的参考(列表)。如果人们认为这对于数据结构而言是不必要的,并且仅将引用返回给子对象的父对象就足够了,那么我会对这些方法的 pros/cons 感兴趣。如您所知,我只是在学习!
有什么想法吗?谢谢!!
如果更多详细信息有用,请告诉我
Data model for Notes - 重要的是:"text" = 注释的字符串和 "establishment" = 保存机构记录名称的参考
Data model for Establishment - 重要的是 "notes" 这是注释项的记录名称的参考(列表)。
GitHub 存储库也在这里:https://github.com/SteveBlackUK/BabiFudTutorial/blob/master/BabiFud/View%20Controllers/NotesTableViewController.swift <这是我正在处理的视图控制器
虽然我仍然有一个悬而未决的问题,即您是否应该在父项和子项中存储父项和子项的引用(可能)...我终于弄清楚了如何将对子项的引用追加回来在父记录中。
提醒:我们正在创建关于某个位置(一个机构)的注释,目标是将注释的引用存储在 CloudKit 中的机构内的引用(列表)中。
//first, make sure we even have an establishment
if self.establishment?.name != nil {
//get the array of note references for the Establishment that we want to update
let noteReferences = CKRecord.Reference(record: noteRecord, action: CKRecord_Reference_Action.deleteSelf)
//store the current array of note references into notesForEstablishment
self.notesForEstablishment = self.establishment?.noteRecords
if self.notesForEstablishment != nil {
if !self.notesForEstablishment!.contains(noteReferences) {
self.notesForEstablishment!.append(noteReferences)
}
} else {
self.notesForEstablishment = [noteReferences]
self.establishment?.noteRecords = self.notesForEstablishment
}
// get the record ID for the current establishment
let establishmentRecordID = self.establishment?.id
//then fetch the establishment from CK
CKContainer.default().publicCloudDatabase.fetch(withRecordID: establishmentRecordID!) { updatedRecord, error in
if let error = error {
print("error handling to come: \(error.localizedDescription)")
} else {
// then update the record and place the array that now holds the reference to the new note into the "notes" Reference (list) in CK
updatedRecord!.setObject(self.notesForEstablishment as __CKRecordObjCValue?, forKey: "notes")
CKContainer.default().publicCloudDatabase.save(updatedRecord!) { savedRecord, error in
}
}
}
}
它可能是更简洁的代码 - 欢迎任何反馈,但它有效!
我正在尝试构建 cloudkit 教程 (https://www.raywenderlich.com/4878052-cloudkit-tutorial-getting-started) 中包含的功能之外的其他功能,并且已经达到了我有一个可以查看机构(餐厅)和关于它们的注释 - 我在 CloudKit 仪表板中创建了数据,并在机构的 'notes' 参考(列表)中预填充了注释的记录名称。
我现在正在扩展该应用程序以允许用户添加注释,并且在我能够创建新注释的位置,并填充 Establishment 的记录名称(在子记录中建立父实体)这个:
let noteRecord = CKRecord(recordType: "Note")
noteRecord["text"] = noteText.text //grabbed from a UIAlert
if self.establishment?.name != nil { //If the establishment exists
let reference = CKRecord.Reference(recordID: self.establishment!.id, action: .deleteSelf)
noteRecord["establishment"] = reference as! CKRecordValue
}
//works fine, saves the new note as expected with the right recordName for the establishment
CKContainer.default().privateCloudDatabase.save(noteRecord) { [unowned self] record, error in
DispatchQueue.main.async {
if let error = error {
} else {
}
}
}
现在我遇到的问题是如何获取这个新保存的注释的记录名称并将其附加到机构的参考(列表)中。
原因是应用程序在教程中的构建方式 - 在获取机构的所有注释时 - 它使用机构的参考(列表)。如果人们认为这对于数据结构而言是不必要的,并且仅将引用返回给子对象的父对象就足够了,那么我会对这些方法的 pros/cons 感兴趣。如您所知,我只是在学习!
有什么想法吗?谢谢!!
如果更多详细信息有用,请告诉我
Data model for Notes - 重要的是:"text" = 注释的字符串和 "establishment" = 保存机构记录名称的参考 Data model for Establishment - 重要的是 "notes" 这是注释项的记录名称的参考(列表)。
GitHub 存储库也在这里:https://github.com/SteveBlackUK/BabiFudTutorial/blob/master/BabiFud/View%20Controllers/NotesTableViewController.swift <这是我正在处理的视图控制器
虽然我仍然有一个悬而未决的问题,即您是否应该在父项和子项中存储父项和子项的引用(可能)...我终于弄清楚了如何将对子项的引用追加回来在父记录中。
提醒:我们正在创建关于某个位置(一个机构)的注释,目标是将注释的引用存储在 CloudKit 中的机构内的引用(列表)中。
//first, make sure we even have an establishment
if self.establishment?.name != nil {
//get the array of note references for the Establishment that we want to update
let noteReferences = CKRecord.Reference(record: noteRecord, action: CKRecord_Reference_Action.deleteSelf)
//store the current array of note references into notesForEstablishment
self.notesForEstablishment = self.establishment?.noteRecords
if self.notesForEstablishment != nil {
if !self.notesForEstablishment!.contains(noteReferences) {
self.notesForEstablishment!.append(noteReferences)
}
} else {
self.notesForEstablishment = [noteReferences]
self.establishment?.noteRecords = self.notesForEstablishment
}
// get the record ID for the current establishment
let establishmentRecordID = self.establishment?.id
//then fetch the establishment from CK
CKContainer.default().publicCloudDatabase.fetch(withRecordID: establishmentRecordID!) { updatedRecord, error in
if let error = error {
print("error handling to come: \(error.localizedDescription)")
} else {
// then update the record and place the array that now holds the reference to the new note into the "notes" Reference (list) in CK
updatedRecord!.setObject(self.notesForEstablishment as __CKRecordObjCValue?, forKey: "notes")
CKContainer.default().publicCloudDatabase.save(updatedRecord!) { savedRecord, error in
}
}
}
}
它可能是更简洁的代码 - 欢迎任何反馈,但它有效!