在一对多关系中获取对象时的谓词问题
我有一个简单的情况,我有两个与多对多关系相关的实体。
两个对象,警报和标签。当我想获取与给定标签关联的所有警报时,我尝试了以下操作:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN tags", theTag];
我得到的是所有警报,而不仅仅是与该标签相关的警报。
然而,尝试另一种方法是可行的:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in %@", theTag.alarms];
由于与代码重用有关的复杂原因,我确实需要第一个方法来工作。任何帮助将不胜感激!谢谢!
I have a simple situation where I have two entities related with Many-To-Many relationship.
Two objects, Alarms and Tags. When I want to fetch all the Alarms associated with a given Tag, I tried this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN tags", theTag];
What I get is all Alarms, not just those related to the Tag.
However, trying this the other way around works:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in %@", theTag.alarms];
For complicated reasons having to do with code reuse, I really need the first one to work. Any help would be much appreciated! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个
Tag
对象,那么您可以通过执行以下操作来获取其所有警报:如果由于某种奇怪的原因您必须使用获取请求来执行此操作(您不应该这样做),您的谓词应该是:
If you have a
Tag
object, then you can get all of its alarms by doing:If for some bizarre reason you have to do this with a fetch request (which you shouldn't), your predicate should be: