Reputation: 39
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)
}
Upvotes: 0
Views: 858
Reputation: 1862
You could try this...
func getData(withCursor cursor: CKQueryCursor? = nil)
{
let cloudContainer = CKContainer.default()
let privateDatabase = cloudContainer.privateCloudDatabase
let operation: CKQueryOperation
if let cursor = cursor
{
operation = CKQueryOperation(cursor: cursor)
}
else
{
let operation_configuration: CKOperationConfiguration = CKOperationConfiguration()
operation_configuration.isLongLived = true
operation_configuration.qualityOfService = .background
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: RemoteFunctions.RemoteRecords.booksDB, predicate: predicate)
operation = CKQueryOperation(query: query)
operation.queuePriority = .veryHigh
operation.configuration = operation_configuration
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 error = error
{
print("Error:", error)
}
else if let cursor = cursor
{
self.getData(withCursor: cursor)
}
else
{
print("Finished with records:")
}
}
}
privateDatabase.add(operation)
}
This is a recursive version of your fuction, using the new CKQueryOperation
API.
It seems that your're loosing reference to the operation object when a cursor arrives.
Upvotes: 4
Reputation: 426
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 above privateDatabase.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.
Upvotes: 0