Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 616
mluisbrown Avatar asked Apr 01 '15 10:04

mluisbrown


1 Answers

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).

like image 91
mluisbrown Avatar answered Nov 04 '22 08:11

mluisbrown