简体   繁体   English

如何在CloudKit中按用户查询?

[英]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. 我可以成功获取当前用户的CKRecordID,但我似乎无法正确查询它。 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. 顺便说一下,我在数据库中创建了几个“MyEntity”记录。

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. 如果您在记录类型中查看CloudKit仪表板并打开下拉菜单“元数据索引字段”,那么您会发现其中没有一个是可搜索的。 Until yesterday they were. 直到昨天他们都是。 This morning I noticed this because I could not sort on creationDate anymore using: 今天早上我注意到了这一点,因为我无法使用以下内容对creationDate进行排序:

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

I hope this is a temporary CloudKit issue that will be fixed soon. 我希望这是一个临时的CloudKit问题,很快就会解决。 Otherwise you have to create a workaround by adding these fields yourself. 否则,您必须自己添加这些字段来创建变通方法。 Besides this I would expect a CloudKit error instead of 0 records. 除此之外,我希望CloudKit错误而不是0记录。

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. 我在我的演示中添加了creatorUserRecordID的查询。 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 如果你想看演示,请看看: https//github.com/evermeer/EVCloudKitDao

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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