对成员 'save(_:completionHandler:)' 的不明确引用以及 CloudKit 保存尝试

Ambiguous reference to member 'save(_:completionHandler:)' with CloudKit save attempt

在更新参考列表并在此代码块的第一行收到错误后,我试图保存回 CloudKit。

Error: Ambiguous reference to member 'save(_:completionHandler:)'

CKContainer.default().publicCloudDatabase.save(establishment) { [unowned self] record, error in
  DispatchQueue.main.async {
    if let error = error {
      print("error handling to come")
    } else {
      print("success")
    }
  }
}

这位于一个功能中,用户将在其中遵循给定位置(机构)。我们正在获取现有机构及其关注者记录,检查所选用户是否在其中,如果不在列表中,则将其附加到列表中(如果关注者列表为空,则创建它)。

Edit, in case helpful
//Both of these are passed in from the prior view controller
  var establishment: Establishment?
  var loggedInUserID: String?

@objc func addTapped() {
    // in here, we want to take the logged in user's ID and append it to the list of people that want to follow this establishment
    // which is a CK Record Reference
    let userID = CKRecord.ID(recordName: loggedInUserID!)
    var establishmentTemp: Establishment? = establishment
    var followers: [CKRecord.Reference]? = establishmentTemp?.followers

    let reference = CKRecord.Reference(recordID: userID, action: CKRecord_Reference_Action.none)
    if followers != nil {
      if !followers!.contains(reference) {
        establishmentTemp?.followers?.append(reference)
      }
    } else {
      followers = [reference]
      establishmentTemp?.followers = followers
      establishment = establishmentTemp
    }

[这是粘贴在问题顶部的 CKContainer.default..... 保存块所在的位置]

我查看了 'ambiguous reference' 上的各种帖子,但无法找出问题的根源。试图明确设置 establisthmentTemp 和追随者的类型以防出现问题(基于其他相关帖子的解决方案)但没有成功。 作为一个相对缺乏经验的新手,我怕是没思路了!

感谢帮助。

记录我想出的解决方案:

两个问题的组合:

  1. 我试图保存 CK 记录的更新版本而不是更新
  2. 我没有将 CK 记录传递给 save() 调用 - 而是一个自定义对象

(I believe point two was the cause of the 'ambiguous reference to member' error)

我通过将保存尝试(问题中的第一段代码)替换为:

解决了这个问题
//first get the record ID for the current establishment that is to be updated
let establishmentRecordID = establishment?.id
//then fetch the item from CK

CKContainer.default().publicCloudDatabase.fetch(withRecordID: establishmentRecordID!) { updatedRecord, error in
  if let error = error {
    print("error handling to come")
  } else {

//then update the 'people' array with the revised one
    updatedRecord!.setObject(followers as __CKRecordObjCValue?, forKey: "people")
    //then save it    
    CKContainer.default().publicCloudDatabase.save(updatedRecord!) { savedRecord, error in
    }
  }
}