I'm just starting out with CloudKit, so bear with me.
Background Information
At WWDC 2015, the apple talked about CloudKit https://developer.apple.com/videos/wwdc/2015/?id=715
In this conversation, they warn about creating a query chain and instead recommend this tactic:
let firstFetch = CKFetchRecordsOperation(...) let secondFetch = CKFetchRecordsOperation(...) ... secondFetch.addDependency(firstFetch) letQueue = NSOperationQueue() queue.addOperations([firstFetch, secondFetch], waitUntilFinished: false)
Structure example
The database of test projects contains pets and their owners, it looks like this:
|Pets | |Owners | |-name | |-firstName | |-birthdate | |-lastName | |-owner (Reference) | | |
My question
I am trying to find all the pets owned by the owner, and I worry that I am creating an apple chain warning of this. See below two methods that do the same, but two ways. Which is more correct or erroneous? I feel like I am doing the same, but instead use only completion blocks.
I am confused about how to change otherSearchBtnClick: use dependency. Where do i need to add
ownerQueryOp.addDependency(queryOp)
in otherSearchBtnClick :?
@IBAction func searchBtnClick(sender: AnyObject) { var petString = "" let container = CKContainer.defaultContainer() let publicDatabase = container.publicCloudDatabase let privateDatabase = container.privateCloudDatabase let predicate = NSPredicate(format: "lastName == '\(ownerLastNameTxt.text)'") let ckQuery = CKQuery(recordType: "Owner", predicate: predicate) publicDatabase.performQuery(ckQuery, inZoneWithID: nil) { record, error in if error != nil { println(error.localizedDescription) } else { if record != nil { for owner in record { let myRecord = owner as! CKRecord let myReference = CKReference(record: myRecord, action: CKReferenceAction.None) let myPredicate = NSPredicate(format: "owner == %@", myReference) let petQuery = CKQuery(recordType: "Pet", predicate: myPredicate) publicDatabase.performQuery(petQuery, inZoneWithID: nil) { record, error in if error != nil { println(error.localizedDescription) } else { if record != nil { for pet in record { println(pet.objectForKey("name") as! String) } } } } } } } } } @IBAction func otherSearchBtnClick (sender: AnyObject) { let container = CKContainer.defaultContainer() let publicDatabase = container.publicCloudDatabase let privateDatabase = container.privateCloudDatabase let queue = NSOperationQueue() let petPredicate = NSPredicate(format: "lastName == '\(ownerLastNameTxt.text)'") let petQuery = CKQuery(recordType: "Owner", predicate: petPredicate) let queryOp = CKQueryOperation(query: petQuery) queryOp.recordFetchedBlock = { (record: CKRecord!) in println("recordFetchedBlock: \(record)") self.matchingOwners.append(record) } queryOp.queryCompletionBlock = { (cursor: CKQueryCursor!, error: NSError!) in if error != nil { println(error.localizedDescription) } else { println("queryCompletionBlock: \(cursor)") println("ALL RECORDS ARE: \(self.matchingOwners)") for owner in self.matchingOwners { let ownerReference = CKReference(record: owner, action: CKReferenceAction.None) let ownerPredicate = NSPredicate(format: "owner == %@", ownerReference) let ownerQuery = CKQuery(recordType: "Pet", predicate: ownerPredicate) let ownerQueryOp = CKQueryOperation(query: ownerQuery) ownerQueryOp.recordFetchedBlock = { (record: CKRecord!) in println("recordFetchedBlock (pet values): \(record)") self.matchingPets.append(record) } ownerQueryOp.queryCompletionBlock = { (cursor: CKQueryCursor!, error: NSError!) in if error != nil { println(error.localizedDescription) } else { println("queryCompletionBlock (pet values)") for pet in self.matchingPets { println(pet.objectForKey("name") as! String) } } } publicDatabase.addOperation(ownerQueryOp) } } } publicDatabase.addOperation(queryOp) }