【问题标题】:Populating parent Reference (List) in CloudKit with child recordName when creating a child record创建子记录时使用子记录名称填充 CloudKit 中的父引用(列表)
【发布时间】: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

【问题讨论】:

    标签: swift cloudkit


    【解决方案1】:

    虽然我仍然有一个悬而未决的问题,即您是否应该将父子引用存储在父子引用中(可能)......我终于能够弄清楚如何将子引用附加到父记录。

    提醒:我们正在创建一个关于位置(一个场所)的备注,目标是将备注的引用存储在 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
    
                }
              }
            }
          }
    

    这可能是更简洁的代码 - 欢迎任何反馈,但它确实有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      相关资源
      最近更新 更多