Get CloudKit records created by specific user

I've been searching all over the web trying to find the proper way to get all records created by a specific user in CloudKit.

I am able to get the correct id using:

guard let userRecordID = try? await container.userRecordID() else { return }

I can see that the id returned is associated with records in my CloudKit dashboard. So I would expect that the following would get those records:

let predicate = NSPredicate(format: "%K == %@", #keyPath(CKRecord.creatorUserRecordID), userRecordID)
let query = CKQuery(recordType: "CKUser", predicate: predicate)

But instead when I use that query it returns nothing. It is successful but with nothing returned...

Any ideas why this would be happening?

P.S. I have also tried constructing the predicate using the reference, but I get the same result - success with no results.

P.S.2 Also worth mentioning that I am trying to get the results from the public database and I have set my CKContainer to the correct container id.

Answered by DTS Engineer in 818893022

... The existing records are not showing when I do the query you suggested previously, but if I create a new record from CloudKit console then it shows in the query. Also if I edit a record using CloudKit console with the iCloud user then it shows...

Yeah, this does seem like CloudKit doesn't index the existing data correctly. In this situation, the best I can suggest is that you file a feedback report to request that the CloudKit team helps re-index your CloudKit container, and follow up with the team from there.

When you file the feedback report, make sure it has the following information:

  • A detailed description of the issue, including the information you provided in this forum post.
  • Your CloudKit container ID.
  • Your developer account name.
  • Your App Store app name and link impacted by this issue, if any.

When you have the feedback report ID, please share here. I'd be happy to route it directly to the team for them to decide what they can do.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

One more thing to note: I have added queryable to the "createdUserRecordName" and sortable for the "createdTimestamp" and "modifiedTimestamp".

Here is the complete code for what I am trying, I modified it a bit from what I originally posted, still returns 0 records even though there are 13 that it should find.

    func fetchRecords(forUserRecordID userRecordID: CKRecord.ID, completion: @escaping (Result<[CKRecord], Error>) -> Void) {
        let reference = CKRecord.Reference(recordID: userRecordID, action: .none)
        let predicate = NSPredicate(format: "creatorUserRecordID == %@", reference)
        let query = CKQuery(recordType: self.reviewRecordType, predicate: predicate)
        
        self.database.configuredWith(configuration: self.config) { db in
            db.fetch(withQuery: query) { result in
                switch result {
                case .success(let success):
                    let results = success.matchResults.compactMap {
                        try? $0.1.get()
                    }
                    
                    completion(.success(results))
                case .failure(let error):
                    completion(.failure(error))
                }
            }
        }
    }

Did you try with CloudKit Console to see if the same query works there? To do so, log in CloudKit Console with your account and use createdUserRecordName + the user record name, which can be found from the Created by field of the target record, as a filter to do the query, as shown in the following screenshots:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

No, that also shows no records. But I can see the the user's id when I just click on the "recordName" of one of the records that should be showing up in the query.

Then my best guess will be that the data is not indexed yet, or for some reason the indexing process can't be finished. In this situation, you might try to remove the createdUserRecordName index of the record type, add it back, then wait for up to several days for CloudKit to finish indexing the data, and try again.

If CloudKit Console still returns no data (while the data is there), you might consider filing a feedback report to see if the CloudKit team can help you re-index your container – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

... The existing records are not showing when I do the query you suggested previously, but if I create a new record from CloudKit console then it shows in the query. Also if I edit a record using CloudKit console with the iCloud user then it shows...

Yeah, this does seem like CloudKit doesn't index the existing data correctly. In this situation, the best I can suggest is that you file a feedback report to request that the CloudKit team helps re-index your CloudKit container, and follow up with the team from there.

When you file the feedback report, make sure it has the following information:

  • A detailed description of the issue, including the information you provided in this forum post.
  • Your CloudKit container ID.
  • Your developer account name.
  • Your App Store app name and link impacted by this issue, if any.

When you have the feedback report ID, please share here. I'd be happy to route it directly to the team for them to decide what they can do.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Get CloudKit records created by specific user
 
 
Q