5

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)`
aksh1t
  • 5,410
  • 1
  • 37
  • 55
Andreas Xenos
  • 335
  • 1
  • 3
  • 7
  • Have you tried something? – Phate01 Apr 16 '15 at 07:16
  • Is your 'Me' field of type CKReference? Then the value that you use in the predicate should be of type CKReference. You are now using recordID. I suspect that field is of type recordID is it? Then you should first create a CKReference out of that. – Edwin Vermeer Apr 16 '15 at 13:18
  • "Me" is just a String and recordID that I am passing to Me is the record ID -> recordName (which is a String). "MyFriend" is a CKReference for which I want retrieve the respective Account Names. – Andreas Xenos Apr 16 '15 at 13:25
  • If MyFriend is a CKReference, then you should not cast it to a string but to a CKReference and then gets it recordID.recordName – Edwin Vermeer Apr 17 '15 at 06:25

1 Answers1

6

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
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58