Steve B
Steve B

Reputation: 600

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

I'm trying to save back to CloudKit after updating a reference list and getting the error on the first line of this code block.

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")
    }
  }
}

This sits within a function where the user going to follow a given location (Establishment). We're taking the existing establishment, and its record of followers, checking to see if the selected user is in it, and appending them to the list if not (or creating it if the list of followers is null).

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
    }

[this is where the CKContainer.default.....save block pasted at the top of the question comes in]

I've looked through the various posts on 'ambiguous reference' but haven't been able to figure out the source of my issue. tried to explicitly set the types for establisthmentTemp and followers in case that was the issue (based on the solutions to other related posts) but no luck. Afraid I'm out of ideas as a relatively inexperienced newbie!

Help appreciated.

Upvotes: 1

Views: 52

Answers (1)

Steve B
Steve B

Reputation: 600

Documenting the solution that I figured out:

Combination of two issues:

  1. I was trying to save an updated version of a CK Record instead of updating
  2. I was not passing a CK Record to the save() call - but a custom object

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

I solved it by replacing the save attempt (first block of code in the question) with:

//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
    }
  }
}

Upvotes: 0

Related Questions