I followed several CKQueryOperation examples/narratives on problems to fetch from CloudKit. My table has about 370 rows and 8 columns..at best I can only fetch about 60 rows. resultsLimit parameter does not seem to help.. My queryCompletionBlock is not executing. Sometimes I fetch 5 rows and other time 30+ Response from Cloud is quick just now all rows It's got to be some newbie code mistake!
func getData() {
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: RemoteFunctions.RemoteRecords.booksDB, predicate: predicate)
let cloudContainer = CKContainer.default()
let privateDatabase = cloudContainer.privateCloudDatabase
let operation = CKQueryOperation(query: query)
operation.queuePriority = .veryHigh
operation.resultsLimit = 20
operation.recordFetchedBlock = { (record: CKRecord) in
self.allRecords.append(record)
print(record)
}
operation.queryCompletionBlock = {[weak self] (cursor: CKQueryCursor?, error: NSError?) in
// There is another batch of records to be fetched
print("completion block called with \(String(describing: cursor))")
if let cursor = cursor {
let newOperation = CKQueryOperation(cursor: cursor)
newOperation.recordFetchedBlock = operation.recordFetchedBlock
newOperation.queryCompletionBlock = operation.queryCompletionBlock
newOperation.resultsLimit = 10
privateDatabase.add(newOperation)
print("more records")
}
// There was an error
else if let error = error {
print("Error:", error)
}
// No error and no cursor means the operation was successful
else {
print("Finished with records:")
}
} as? (CKQueryCursor?, Error?) -> Void
// privateDatabase.add(operation)
}
Remove the
as? (CKQueryCursor?, Error?) -> Void
near the end. Be careful not to remove the preceeding brace.Remove
newOperation.resultsLimit = 10
in your cursor block.Add
operation = newOperation
immediately aboveprivateDatabase.add(newOperation)
Uncomment the
privateDatabase.add(operation)
That should help. Large fetches, where the cursor block gets hit more than 3 times can be problematic. If you do the above, you should be ok. Some people like to write/call the cursor block as its own function. That works as well, but it isn't necessary.