Search code examples
iosxcodecloudkitckquery

Can I receive count of records corresponding to CKQuery from CloudKit?


I try to use CKQueryOperation for this:

CKQueryOperation *o = [[CKQueryOperation alloc] initWithQuery:query];
[o setDesiredKeys:@[]];

But it still returns a lot of data that I don't need in this moment

<CKRecord: 0x79881a50; recordType=Comment, recordID=CBB7B269-47AH-406E-8934-682D31501778:(_defaultZone:__defaultOwner__), recordChangeTag=i1x8j1ey, values={
}>
{
    creatorUserRecordID -> <CKRecordID: 0x7966e9d0; _e5b178bde573ba6fee336b9ed8:(_defaultZone:__defaultOwner__)>
    lastModifiedUserRecordID -> <CKRecordID: 0x7966cdd0; _e5b178bd3e9e7a6e336b9f34ed8:(_defaultZone:__defaultOwner__)>
    creationDate -> 2014-10-31 07:20:45 +0000
    modificationDate -> 2014-10-31 07:20:45 +0000
    modifiedByDevice -> iPhone Simulator
}

I need only a count of records.


Solution

  • Aggregation queries are not possible in CloudKit. So you have to query all records and count those. To make sure that all records will be returned, you have to set operation.resultsLimit to a value larger than the count otherwise it could happen that not all records are returned.