具有多对多关系的 CoreData NSPredicate

发布于 2024-09-07 07:04:43 字数 459 浏览 0 评论 0原文

我已经建模了人员/标签关系。这是多对多的关系;一个人可以有多个标签,一个标签可以关联多个人。

我正在尝试运行一个查询来查看已标记有一组标签中的每个标签的人员列表。例如:每个被标记为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娜些时光,永不杰束 2024-09-14 07:04:43

反转您的查询并询问“self in %@”的标签,然后您可以对结果数组执行 KVC 操作,例如:

NSArray *array = [results valueForKeyPath:@"@distinctUniionOfArrays.person"];

这将为您提供您正在寻找的结果。  请注意,这是在我的 iPad 上输入的,因此可能存在拼写错误。

请参阅有关此主题的文档

http://developer.apple.com/mac/library/iPad/index.html#documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html

更新

抱歉,我误读了这个问题。我建议尝试子查询。如果无法访问您的数据结构,则很难对其进行测试以使其完全正确,但此代码应该使您走上正确的道路:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(self.tags, $tag, ALL $tag.name in %@).count > 0)", tagNameArray]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
NSAssert2(error == nil, @"Error fetchings tags: %@\n%@", [error localizedDescrption], [error userInfo]);

Reverse your query and ask for tags that are "self in %@" and you can then perform a KVC operation on the resulting array like:

NSArray *array = [results valueForKeyPath:@"@distinctUniionOfArrays.person"];

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:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(self.tags, $tag, ALL $tag.name in %@).count > 0)", tagNameArray]];

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
NSAssert2(error == nil, @"Error fetchings tags: %@\n%@", [error localizedDescrption], [error userInfo]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文