Quentin Malgaud
Quentin Malgaud

Reputation: 415

cloudkit enter userID as reference of a new record (in code)

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!

Upvotes: 2

Views: 656

Answers (1)

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

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)

Upvotes: 3

Related Questions