Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query CloudKit by lastModifiedUserRecordID?

Tags:

swift

cloudkit

Based on Apple documentation, lastModifiedUserRecordID, is a system field that's auto generated on iCloud.

I want to fetch records from the public iCloud database that where last modified by me.

I set "modifiedBy" as Queryable on the CloudKit Dashboard and added this predicate:

let userId = CKRecord.Reference(recordID: userCKRecordID, action: .none)
lastModifiedPred = NSPredicate(format: "lastModifiedUserRecordID = %@", userId)

And it returns no results. If I remove this predicate, it returns 1 result and I can see in the dashboard that the last modified by record id (recordName) matches with the user recordName I'm sending it.

Oddly enough, using "creatorUserRecordID" and passing the same value as above does return proper results but I need lastModifiedBy.

EDIT: Per the suggestions I tried to use the actual userCKRecord.recordName as a string and sending this predicate to CloudKit.

lastModifiedPred = NSPredicate(format: "lastModifiedUserRecordID = %@", userCKRecord.recordName)

and received this error:

"Field \'___modifiedBy\' has a value type of REFERENCE and cannot be queried using filter value type STRING"

As I mentioned above, I can query creatorUserRecordID just fine using the original predicate at the top (using CKRecord.ID), but it just doesn't work for lastModifiedUserRecordID.

like image 824
William T. Avatar asked Oct 16 '22 13:10

William T.


1 Answers

Since you want to fetch records that were last modified by you, you have to get first your UserRecordID. This is done by calling

fetchUserRecordID(completionHandler: { (myUserRecordID, error) in
… // see below
}  

see the docs. This way, you get your user myUserRecordID.
With this, you can setup your query predicate:

let lastModifiedPred = NSPredicate(format: "lastModifiedUserRecordID = %@", myUserRecordID)  

and execute your query.

like image 58
Reinhard Männer Avatar answered Nov 15 '22 13:11

Reinhard Männer