swifticloudcloudkitckreference

Cannot assign userID as reference


I am trying to assign several different attributes to a record.

Date & Name are working properly. Avatar, which is a reference to another record, works also fine. But then I try to assign a reference to the user and I get the slamyourheadagainstthescreen moment. Here is my code:

var avatarID : CKRecordID!
var userID : CKRecordID!

//avatarID has been assigned value in another function
daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
println("this is avatarID: \(avatarID!)")

containerY.fetchUserRecordIDWithCompletionHandler{
            userRecordID, error in
            if error != nil {
                println("error")
            } else {
                println("gettin close")
                self.userID = userRecordID
                println("this is userID: \(self.userID!)")
                daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
                }
            }
        publicDatabase.saveRecord(daMainUser) {
            record, error in
            if error != nil {
                println(error.localizedDescription)
            } else {
                println(record.objectForKey("nickName") as String + " recorded")
            }
        }

When I save daMainUser and go have a look in apple dashboard I see that "avatar" reference was successfully saved but not "ownerOfThat". Here is what I get from the console:

this is avatarID: <CKRecordID: 0x7a83d3d0; 79CE1EDC-593D-4AE0-8433-8488F857A302:(_defaultZone:__defaultOwner__)>
gettin close
this is userID: <CKRecordID: 0x7a863910; _8a15e8e6138ba234b62f25cfd3eeb66e:(_defaultZone:__defaultOwner__)>
John Doe recorded

That makes me believe that the userID and an ID from another record are not the same kind of ID. But I don't know why and what to do. Thank you in advance


Solution

  • I think you have your brackets wrong. It should be:

    var avatarID : CKRecordID!
    var userID : CKRecordID!
    
    //avatarID has been assigned value in another function
    daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
    println("this is avatarID: \(avatarID!)")
    
    containerY.fetchUserRecordIDWithCompletionHandler{
                userRecordID, error in
                if error != nil {
                    println("error")
                } else {
                    println("gettin close")
                    self.userID = userRecordID
                    println("this is userID: \(self.userID!)")
                    daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
                }
            publicDatabase.saveRecord(daMainUser) {
                record, error in
                if error != nil {
                    println(error.localizedDescription)
                } else {
                    println(record.objectForKey("nickName") as String + " recorded")
                }
            }
         }
    

    Now you save the record before it's fetched.