I create a series of CK references with a contact having mutiple locations, each locaiton ahving a provider and each provider having a meter and so forth let self.currentLocationCkRecord["ownningContact"] = CKReference(record: self.currentContactCkRecord!, action: CKReferenceAction.deleteSelf)
let self.currentProviderCkRecord["ownningLocation"] = CKReference(record: self.currentLocationCkRecord!, action: CKReferenceAction.deleteSelf)
let self.currentMeterCkRecord["ownningProvider"] = CKReference(record: self.currentProviderCkRecord!, action: CKReferenceAction.deleteSelf)
when I retrieve all the records including referenced records I run into an issue if nesting code to get each of the referenced records
let predicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "Contact", predicate: predicate)
privateDB?.perform(query, inZoneWith: self.ckRecordZoneID) { (records, error) in
// handle error
if let records = records {
for aRecord in records {
// I process location CKRecord
self.alliCloudLocations.append(record)
let contactID = aRecord.recordID
let recordToMatch = CKReference(recordID: contactID, action: .deleteSelf)
let predicate = NSPredicate(format: "owningContact == %@", recordToMatch)
// Create the query object.
let query = CKQuery(recordType: Cloud.Entity.Location, predicate: predicate)
let ckQueryOpLocation = CKQueryOperation(query: query)
ckQueryOpLocation.queryCompletionBlock = { (cursor, error) in
print("Query completion block called")
guard error == nil else {
if let ckerror = error as? CKError {
self.aErrorHandler.handleCkError(ckerror: ckerror)
}
return
}
if cursor != nil {
let nextQueryOp = CKQueryOperation(cursor: cursor!)
nextQueryOp.recordFetchedBlock = = { (record: CKRecord) in
self.alliCloudLocations.append(record)
print(record)
// TO DO: I need to get a provider CKRecord and for each location CKRecord and for each provider CKRecord I ned to get a meter CKRecord
}
nextQueryOp.ZoneID = self.CKRecordZoneID
nextQueryOp.queryCompletionBlock = ckQueryOpLocation.queryCompletionBlock
nextQueryOp.desiredKeys = = ["locationName"]
nextQueryOp.desiredKeys = = ["zip"]
nextQueryOp.desiredKeys = = ["locationType"]
nextQueryOp.resultsLimit = ckQueryOpLocation.resultsLimit
//important
ckQueryOpLocation = nextQueryOp
privateDB.add(ckQueryOpLocation)
print("added next fetch")
}
}
}
}
// Add the CKQueryOperation to a queue to execute it and process the results asynchronously.
privateDB?.add(ckQueryOpLocation)
}
In the above code for each Contact CKRecord, I am fetching location CKRecords and then as you can see from my above // TO DO comment statement: I need to call the entire perform CKQuery and QueryCompletionBlock for each of the referenced records: provider, and meter
My question is when I pull the location CKRecord does it pull all the referenced Provider CKRecord and Meter CKRecord; if so how to retrieve each of them or Do I have to fetch each of the Provider and Meters CKRecords individually and if so code gets very complicated inside the recordFetchedBlock method since that is where I have to call the nested code.
Can anyone advise how to structure this code in a simple and easy to operate manner?