Rui Lopes
Rui Lopes

Reputation: 2562

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]];

enter image description here

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? How?

(i'm newbie in coredata...)

Thanks,

RL

Upvotes: 1

Views: 2083

Answers (1)

Massimo Cafaro
Massimo Cafaro

Reputation: 25429

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.

Upvotes: 4

Related Questions