CoreData - NSPredicate results if relationship has data
I have a many-to-many relationship between tables, and I populate a tableView with Activities. For that i user a simple NSPredicate like this:
request.predicate = [NSPredicate predicateWithFormat:@"deleted == %@", [NSNumber numberWithBool:NO]];
How can I do to show only the Activities that has Members attached to it?
I think that in the NSPredicate I have to do some count so that only the Activities with count > 0 are returned. Is that so? H开发者_如何学Pythonow?
(i'm newbie in coredata...)
Thanks,
RL
You need to add a subquery to your predicate acting on the CompanyActivity entity as follows:
[[NSPredicate predicateWithFormat:@"deleted == %@" && (0 >= SUBQUERY(Members, $sub, $sub.deleted == %@).@count)", [NSNumber numberWithBool:NO] [NSNumber numberWithBool:NO]];
The first part of the predicate returns objects which have not been deleted, the second one related to the subquery will take care of retrieving all those CompanyActivity objects whose Members have not been deleted.
精彩评论