简体   繁体   中英

How to query CloudKit by lastModifiedUserRecordID?

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.

You are using NSPredicate to compare a string literal. Where userId is not a string literal. Use this.

lastModifiedPred = NSPredicate(format: "lastModifiedUserRecordID = %d", INT-VALUE-OF-userId)

Based on apple documentation on https://developer.apple.com/documentation/cloudkit/ckrecord/id

CKRecord.ID -> class ID : NSObject

A record ID object consists of a name string and a zone ID. The name string is an ASCII string not exceeding 255 characters in length.

comparing an object and string won't give you the result, try to cast the CKRecord.ID as String maybe help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM