iosswiftcloudkitckreference

How to fetch reference string from CKReference object


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?


Solution

  • 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")
    }