4

我创建了一系列 CK 引用,其中一个联系人具有多个位置,每个位置都有一个提供者,每个提供者都有一个仪表等等 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)
    }

在每个联系人 CKRecord 的上述代码中,我正在获取位置 CKRecords,然后从我上面的 // TO DO 注释语句中可以看到:我需要为每个引用的记录调用整个执行 CKQuery 和 QueryCompletionBlock:提供程序和仪表

我的问题是,当我拉出位置 CKRecord 时,它会拉出所有引用的 Provider CKRecord 和 Meter CKRecord;如果是这样,如何检索它们中的每一个,或者我是否必须单独获取每个 Provider 和 Meters CKRecords,如果是这样,则 recordFetchedBlock 方法中的代码变得非常复杂,因为这是我必须调用嵌套代码的地方。

谁能建议如何以简单易操作的方式构造此代码?

4

0 回答 0