CloudKit count records
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.