【发布时间】:2020-01-17 22:20:09
【问题描述】:
我正在尝试构建超出 cloudkit 教程 (https://www.raywenderlich.com/4878052-cloudkit-tutorial-getting-started) 中包含的其他功能,并且已经到了我有一个工作应用程序可以查看机构(餐厅)和关于它们的注释的地步 - 我已经在 CloudKit 仪表板中创建数据,并在机构的“注释”参考(列表)中预填充注释的记录名称。
我现在正在扩展应用程序以允许用户添加注释,并且在我能够创建新注释的地方,并使用以下内容填充机构的记录名称(在子记录中建立父实体):
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 {
}
}
}
现在我遇到的问题是如何获取这个新保存的笔记的记录名称并将其附加到机构的参考(列表)中。
原因是本教程中构建应用程序的方式 - 在获取机构的所有注释时 - 它使用机构的参考(列表)。如果人们认为这对于数据结构来说是不必要的,并且只需将引用返回给孩子的父母就足够了,我会对这些方法的优缺点感兴趣。如您所知,我只是在学习!
有什么想法吗?谢谢!!
如果更多细节有用,请告诉我
Data model for Notes - 重要的是:“text” = 注释字符串,“establishment” = 保存机构记录名称的参考 Data model for Establishment - 重要的是“笔记”,它是笔记项目的记录名称的参考(列表)。
这里也有 GitHub 存储库:https://github.com/SteveBlackUK/BabiFudTutorial/blob/master/BabiFud/View%20Controllers/NotesTableViewController.swift
【问题讨论】: