开发者

Predicate Problem when fetching objects in a To-Many relationship

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.

How开发者_开发问答ever, 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!


If you have a Tag object, then you can get all of its alarms by doing:

NSSet *alarms = [theTag alarms];

If for some bizarre reason you have to do this with a fetch request (which you shouldn't), your predicate should be:

NSPredicate *p = [NSPredicate predicateWithFormat:@"tags CONTAINS %@", theTag];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜