6

Using CloudKit on iOS, is it possible to determine the number of records that a particular query matches?

I'm not actually interested in reading the records themselves, I simply want to know how many records are matched by a query. I'm aware that matched records are returned in batches, so in principle I could obtain each batch of records, and calculate the total number of records by summing the number in each batch. However, the number of matched records will likely be large for this application, and given I'm not interested in the actual record content, this seems inefficient and wasteful.

Thanks in advance.

Chris
  • 941
  • 9
  • 18

1 Answers1

4

There are no aggregation functions in CloudKit. The only way to do a count is by fetching all records.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Thanks for your response, Edwin. – Chris Feb 19 '15 at 11:30
  • Is this still relevant or there's any updated regarding this? It's kinda weird we having to fetch data just for a count. I'm always reducing the amount of fetched data to a single field like `createdAt` then I can count the number of objects. – Ivan Cantarino Jul 02 '18 at 08:47
  • I found that requesting a very large (say 1 million) number of records will return the actual count along with the error message that you cannot request more than 400 records at once. So you can fake it with a call you know will fail. – john elemans Sep 17 '19 at 21:02
  • From this post: https://stackoverflow.com/questions/24191999/cloudkit-count-recordsAlso, 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. – Steve B Aug 28 '20 at 20:21