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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With