0

I have two record type: Books & Authors. Books have two attribute BookName(String) & AuthorName(CKReference). Authors have one attribute AuthorTitle(String).

I save CKReference as follows

let authorReference = CKReference(record: addBookViewController.authorTitle!, action: CKReferenceAction.DeleteSelf)

bookRecord.setObject(authorReference, forKey: "AuthorName")

I am currently fetching CKReference as follows to display on table

let authorReference = bookRecord.objectForKey("AuthorName") as! CKReference

let authorTitle = authorReference.recordID.recordName

cell.detailTextLabel?.text = authorTitle

On tableview cell it shows record ID of AuthorName. Instead I want to see AuthorName in string format. How do I get it?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Apoorv Mote
  • 523
  • 3
  • 25
  • please show how addBookViewController.authorTitle is declared – harryhorn May 30 '15 at 08:41
  • its very complicated & requires long explanation. But I assure you that addBookViewController.authorTitle is a CKRecord of Authors. When I display authorReference.recordID.recordName on tableview I am getting correct Authors record ID as String. But I want fetch actual Authors record with this ID & show AuthorsTitle String attribute. – Apoorv Mote May 30 '15 at 09:42

2 Answers2

2

You can also use CKFetchRecordsOperation

CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:@"firstRecordIDName"];
CKRecordID *recordID2 = [[CKRecordID alloc] initWithRecordName:@"secondRecordIDName"];
NSArray *arrayOfRecordIDs = @[recordID,recordID2];;
CKFetchRecordsOperation *fetchRecordsOperation = [[CKFetchRecordsOperation alloc]initWithRecordIDs:arrayOfRecordIDs];
fetchRecordsOperation.perRecordCompletionBlock = ^(CKRecord *record, CKRecordID *recordID, NSError *error) {
    if (error) {

        // handle error

    }else {

        // Successfull fetch, create data model

    }

};
fetchRecordsOperation.database = publicDatabase;
[fetchRecordsOperation start];
1

Your AuthorName field is a reference to an other record. In order to get data from that record, you need to fetch it first.

database.fetchRecordWithID(CKRecordID(recordName: authorReference.recordID), completionHandler: {record, error in
 // now you can use record.objectForKey("AuthorTitle")
}
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58