mluisbrown
mluisbrown

Reputation: 14928

NSPredicate expression to filter on count of a to-many relationship

I have a Core Data model with a one-to-many relationship e.g.:

@interface Person : NSManagedObect
@property (nonatomic, retain) NSSet *children;
@end

I want to create a predicate which only gives me the Persons that have at least one child:

I tried: [NSPredicate predicateWithFormat:@"person.children.count > 0"]

But I get NSPredicate to-many key not allowed.

Upvotes: 9

Views: 5249

Answers (1)

mluisbrown
mluisbrown

Reputation: 14928

Ok, I found some documentation on the realm.io site about NSPredicate collection queries which has the answer:

You have to use @count instead of just count:

So: [NSPredicate predicateWithFormat:@"person.children.@count > 0"]

Pity that Apple doesn't document this themselves (at least not that I could find).

Upvotes: 29

Related Questions