2

I am setting an app in which a user could create records. Let's say he/she is the owner of a chatroom. Therefore there is an attribute called "admin" in the chatroom record. This attribute takes a reference which is the ID of the owner.

Here is what I tried:

CKContainer.defaultContainer().fetchUserRecordIDWithCompletionHandler({
            userRecordID, error in
            if error != nil {
                println("caca")
            } else {
                println("gettin close")
                let UserRecordIDToStore = NSKeyedArchiver.archivedDataWithRootObject(userRecordID)
                let iDString = userRecordID.recordName as String
                daMainUser.setObject(iDString, forKey: "user")
            }
            })

When I pass iDString as above, and I create the record (the room), its admin reference is empty. Whether I did cast iDString as a String or not.

When I pass userRecordID directly, I get an error: 'CKRecordID' is not identical to 'CKRecordValue'

I have been looking everywhere but I cannot find more information about this.

Any help would be greatly appreciated. Thank you guys!

Quentin Malgaud
  • 405
  • 6
  • 21

1 Answers1

3

If your user field is set up as a CKReference field, then you should set it like this:

daMainUser.setObject(CKReference(recordID: userRecordID, action: CKReferenceAction.None), forKey: "user")

In your case you do not have to create a recordID because you already have it. Otherwise you had to create it with something like:

var recordID = CKRecordID(recordName: userRecordID.recordName)
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Hi Edwin, thank you very much for your answer. The problem is that when I enter open the parentheses after CKReference, I can't choose (recordID: ..., forKey: ...) Instead I can only choose 1: CKReference(coder: ...) 2: CKReference(record: ..., action: ...) 3: CKReference(recordID: ..., action: ...) When I enter daMainUser.setObject(CKReference(recordID: userRecordID, forKey: "user") I get an error: Extra argument 'recordID' in call My version of Xcode is 6.1.1 don't know if that can help. – Quentin Malgaud Feb 12 '15 at 16:08
  • sorry, that was a cut and past bug. I fixed the answer. – Edwin Vermeer Feb 12 '15 at 19:59
  • Thank you very much for your answer. I appreciate it. But it seems like I am facing bad luck on this one. I entered the correct line from above, but the "user" reference of the record is still empty, while other attributes (date, name) where successfully saved. Any idea? – Quentin Malgaud Feb 13 '15 at 01:35