Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CKReference on CloudKit using Swift

I have a RecordType Account and another one Friends. For Account I am setting the RecordID on creation of each record and I have a column Name.

For Friends I have two columns, Me and MyFriend. MyFriend is a Reference containing the RecordID of an Account.

How can I retrieve the Names of Accounts that are my friends? I need to query Friends to get all the records where Me is my RecordID and then I need to use these references of the records to get the Names from the Account table.

I have tried the following but I get no results:

let predicate = NSPredicate(format: "Me = %@", recordID)
let query = CKQuery(recordType: "Friends", predicate: predicate)
publicDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
    if error != nil {
        println("Error fetching from Friends table: \(error.description)")
    } else {
        for friend in results {
            var key = friend.objectForKey("MyFriend") as String
            self.publicDB.fetchRecordWithID(CKRecordID(recordName: key), completionHandler: { (record, error) -> Void in
                if error != nil {
                    println(error.description)
                } else {
                    self.friends.append(record.objectForKey("Name") as String)
                }
            })
        }
        dispatch_semaphore_signal(self.semaphore)
    }
 }
 dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER)`
like image 247
Andreas Xenos Avatar asked Apr 16 '15 07:04

Andreas Xenos


1 Answers

If MyFriend is a CKReference, then you should not cast it to a string but to a CKReference and then gets it recordID.recordName. So that code will look like this:

var myFriend = friend.objectForKey("MyFriend") as CKReference
var key = myFriend.recordID.recordName
like image 105
Edwin Vermeer Avatar answered Nov 07 '22 19:11

Edwin Vermeer