0
votes

I have the following predicate:

let predicate = NSPredicate(format: "NOT (recordID in %@)", recordIDs)
-- recordIDs is an array of CKRecordID objects corresponding to the CKRecords on the device

...that produces a runtime error about the predicate. If I change the predicate format string to something else, the query runs fine. I have the "Query" checkbox checked for all the metadata for this record type in CloudKit.

According to CKQuery documentation:

Key names used in predicates correspond to fields in the currently evaluated record. Key names may include the names of the record’s metadata properties such as "creationDate” or any data fields you added to the record.

According to CKRecord documentation, these are the available metadata for querying:

recordID, recordType, creationDate, creatorUserRecordID, modificationDate, lastModifiedUserRecordID, recordChangeTag

3

3 Answers

1
votes

You can use the creation date:

NSPredicate(format: "creationDate > %@", dateLastFetched)

After you pull the records down to the device and save them, save the dateLastFetched and use it for subsequent fetches.

Edit: Be sure to enable the creationDate query index on the CloudKit dashboard (it is not enabled by default like many other indexes)

1
votes

This is an old question, so I'm not sure if it existed at the time, but the correct way to do this now is to use Apple's built-in server change token support.

Making a giant query including all existing record ID's on the device is going to be slow, and picking a date is going to be imprecise.

The right way to do this is to use CKFetchRecordZoneChangesOperation and pass in a CKServerChangeToken using the operation's configurationsByRecordZoneID property.

The first time you call, pass a nil change token. As records are received, CloudKit will call recordZoneChangeTokensUpdatedBlock with the latest change token. Persist the latest token so the next time you need records from the server, you can just pass in your most recent token and get only what's changed since then.

0
votes

Enable the meta data index by clicking here:

enter image description here