Stultus Stultus - 3 months ago 18x
iOS Question

CloudKit CKReference List

So I hava record that contains a field which is an CKReference List.

Now I would like to fetch all referenced(by this list of CKReferences) records(from another type) in batch.

Any Ideas?


Let's say you have a record "Company" and they have a CKReference List field named "Employees".

Doing the fetch for the Company is easy but once you have the companyRecord, you now want to fetch its Employees. You can do it like this:

//first you need to create an array of employee record IDs
var employeeRecordIds = [CKRecordID]()
for employeeReference in companyRecord.objectForKey("Employees") as! [CKReference] {
//now you can fetch those employees
var fetchOperation = CKFetchRecordsOperation(recordIDs: employeeRecordIds)
fetchOperation.fetchRecordsCompletionBlock = {
    records, error in
    if error != nil {
    } else {
        for (recordId, record) in records {

Please note that, while it's sometimes unavoidable with many-to-many relationships, in one-to-many relationships you should ALWAYS try to only use "back references" in CloudKit.

So, in this example, it's encouraged that you put a Company reference on the Employee record, rather than an Employee reference list on the Company record.