Fetch Record Assets in CloudKit Using Swift

谁都会走 提交于 2019-12-06 16:17:53

You could get the record directly if you know the recordId by performing a fetch like:

database.fetchRecordWithID(CKRecordID(recordName: recordId), completionHandler: {record, error in

Or if you don't know the ID you should query for the record with something like the code below. just create the right NSPredicate. A query can return more than 1 record.

var query = CKQuery(recordType: recordType, predicate: NSPredicate(value: true))
var operation = CKQueryOperation(query: query)
operation.recordFetchedBlock = { record in
    // is this your record...
}
operation.queryCompletionBlock = { cursor, error in
    self.handleCallback(error, errorHandler: {errorHandler(error: error)}, completionHandler: {
        // ready fetching records
        })
}
operation.resultsLimit = CKQueryOperationMaximumResults;
database.addOperation(operation)

In your case when using the fetchRecordWithID option the code would look like this:

override func viewDidLoad() {
    super.viewDidLoad()

    database.fetchRecordWithID(CKRecordID(recordName: recordId), completionHandler: {record, error in
        self.myRecord = record
        loadCoverPhoto() { photo in
            dispatch_async(dispatch_get_main_queue()) {
                self.theImage.image = photo
            }
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!