具有多对多关系的 CoreData NSPredicate
我已经建模了人员/标签关系。这是多对多的关系;一个人可以有多个标签,一个标签可以关联多个人。
我正在尝试运行一个查询来查看已标记有一组标签中的每个标签的人员列表。例如:每个被标记为“FOO”和“BAR”的人。
这是我尝试过的代码,但没有成功。
NSPredicate *attributePredicate = [NSPredicate predicateWithFormat:
@"ALL personTags.tagName in %@", filtersArray];
[subPredicates addObject:attributePredicate];;
有什么办法解决这个问题吗?我使用 SqlLite 作为持久存储。 我的数据库中有大约 2000 个人和大约 100 个不同的标签,只有少数会同时应用标签。
I have modeled a Person/Tag relationship. It's a many to many relationship; a person can have multiple tags and a tag can be associated to multiple people.
I am trying to run a query to see get a list of people, that have been tagged with EVERY tag of a set of tags. For Example: Every person that has been tagged with 'FOO' and 'BAR'.
This is the code I was trying with no success.
NSPredicate *attributePredicate = [NSPredicate predicateWithFormat:
@"ALL personTags.tagName in %@", filtersArray];
[subPredicates addObject:attributePredicate];;
Is there any way around this? I'm using SqlLite as the persistent store.
I have around 2000 people in the database and around 100 different tags, only a few would tags applied at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反转您的查询并询问“self in %@”的标签,然后您可以对结果数组执行 KVC 操作,例如:
这将为您提供您正在寻找的结果。 请注意,这是在我的 iPad 上输入的,因此可能存在拼写错误。
请参阅有关此主题的文档
http://developer.apple.com/mac/library/iPad/index.html#documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html
更新
抱歉,我误读了这个问题。我建议尝试子查询。如果无法访问您的数据结构,则很难对其进行测试以使其完全正确,但此代码应该使您走上正确的道路:
Reverse your query and ask for tags that are "self in %@" and you can then perform a KVC operation on the resulting array like:
Which will give you the results you are looking for. Note that this was typed onto my iPad so typos are probable.
See the documentation on this subject
http://developer.apple.com/mac/library/iPad/index.html#documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html
Update
Sorry, I had misread the question. I would suggest trying a subquery. Without access to your data structure it is hard to test this to get it exactly right but this code should put you on the right path: