I have a "table" that can potentially have many records, when adding a new record I need to know how many records there already are in current table as I use it in calculation of some values. The closest thing I could find is requesting all entries like this:
var query : CKQuery = CKQuery(recordType: "Stars", predicate: NSPredicate(format: "mass > 0"))
var request : CKQueryOperation = CKQueryOperation(query: query)
var starCount = 0
request.queryCompletionBlock = {
(cursor:CKQueryCursor!, error:NSError!) in
if error {
completionHandler(ECOResponse.error(error.description), starCount)
} else {
completionHandler(ECOResponse.ok(), starCount)
}
}
request.recordFetchedBlock = {
(record:CKRecord!) in
starCount += 1
}
I wish queryCompletionBlock gave a count or results array along with CKQueryCursor, but unfortunately it does not.
Is there any other way to calculate number of rows in the table?
No, it's not possible to get the total number of records that comply to your query. Also by enumerating the result the answer could be wrong. The number of records returned by CloudKit is not fixed. CloudKit has a mechanism for deciding how many records to return. It is possible to set this to a fixed number on the CKQueryOperation object. The default is:
operation.resultsLimit = CKQueryOperationMaximumResults;
The documentations for this property says:
When using that value, the server chooses a limit that aims to provide an optimal number of results that returns as many records as possible while minimizing delays in receiving those records. However, if you know that you want to process a fixed number of results, change the value of this property accordingly.
If your count is the same as this fixed number, then there are probably more records than your query returned.
It's not possible to get the total number of records that match a query in a non-iterative manner, but it is possible to do it iteratively.
CKQueryOperation
will allow you to fetch all the possible results of a query, however, potentially only through multiple subsequent operations: If the search yields many records, the operation object may deliver a portion of the total results to your blocks immediately, along with a cursor for obtaining the remaining records.
source: CKQueryOperation You can just disregard all CKRecord
instances returned through CKQueryOperation
recordFetchedBlock
and simply increment your own NSUInteger
counter. In my testing, the total number of times recordFetchedBlock
is invoked always matches the number of query results expected.
Also, for efficiency, you can use the desiredKeys
property of CKQueryOperation
in order to limit the amount of data retrieved for each record during the search operation
. source: desiredKeys Comments inside the CKQueryOperation.h
source file state that If set to an empty array, declares that no user fields should be downloaded.
I tested the scenario above with CKQueryOperation
successfully counting through hundreds of records, thus above the default CKQueryOperationMaximumResults
batch limit of 100.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With