CloudKit - NSPredicate for finding all records that contain specified CKReference in a reference list

余生长醉 提交于 2020-01-01 00:45:14

问题


I am working on a CloudKit backed app with a Users record type that has a "following" reference list attribute. I am trying to construct a query to get every user that is following a specified user (i.e. those users in which the specified user appears as an entry in the following reference list).

I am currently trying to construct my NSPredicate for the CKQuery as such:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN following", [[CKReference alloc] initWithRecordID:currentUserID action:CKReferenceActionNone]];
CKQuery *query = [[CKQuery alloc] initWithRecordType:UserRecordType predicate:predicate];

And CloudKit returns the following error message:

"Unknown Item" (11/2003); server message = "did not find required record type";

I feel I might be missing something pretty straight forward in my predicate. Any help would be greatly appreciated!


回答1:


Use CONTAINS operator to test list membership:

CKReference* recordToMatch = [[CKReference alloc] initWithRecordID:currentUserID action:CKReferenceActionNone];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"following CONTAINS %@", recordToMatch];

If you want to query for more than 1 reference, then you need to use a compound predicate with AND predicate type for each reference in list.




回答2:


Error 11 means that the query cannot find any items in your container matching your record type. In your case UserRecordType type records do not exist in Cloud Kit. You can create a sample schema for that type and the query will work after that.



来源:https://stackoverflow.com/questions/25189158/cloudkit-nspredicate-for-finding-all-records-that-contain-specified-ckreferenc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!