简体   繁体   中英

How to query by user in CloudKit?

I want to query for all records created by the current user. I can successfully get the CKRecordID of the current user, but I can't seem to query with it correctly. Below is the code I am using. What am I doing wrong?

[[CKContainer defaultContainer] fetchUserRecordIDWithCompletionHandler:^(CKRecordID *recordID, NSError *error) {
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"creatorUserRecordID = %@", recordID];
     CKQuery *query = [[CKQuery alloc] initWithRecordType:@"MyEntity" predicate: predicate];
     [database performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
           //results are empty       
      }];
}];

By the way, I have created several "MyEntity" records in the database.

If you look in the CloudKit Dashboard at your record type and open the drop down 'Meta data Index fields' then you see that none of those are searchable. Until yesterday they were. This morning I noticed this because I could not sort on creationDate anymore using:

[NSSortDescriptor(key: "creationDate", ascending: false)]

I hope this is a temporary CloudKit issue that will be fixed soon. Otherwise you have to create a workaround by adding these fields yourself. Besides this I would expect a CloudKit error instead of 0 records.

update: I just noticed that it is possible to set the fields sortable and queryable again. I just tried this and it works. I added a query for creatorUserRecordID to my demo. The thing is that only new records since these are set will be returned. Old records won't show up. If you want to see the demo, then have a look at: https://github.com/evermeer/EVCloudKitDao

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