Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with negating Core Data NSPredicate relationships

I'm scratching my head on this one. I have a work around, but I don't understand it so that doesn't count. What I want to do is for the entity (in this case a "Photo" lets say), I want to find all the Photos reviewed by anyone OTHER than the specified user. The relationship here is Photo->Review->User, where a photo can have multiple reviews, and each review is owned by exactly one user. The first two examples were my logical first attempts, but does not work. I found some similar code that shows the subquery which works, but can anyone explain why the first two examples don't work?

// this does not work
[NSPredicate predicateWithFormat:@"NOT (ANY reviews.user = %@)", self.user]

// this does not work
[NSPredicate predicateWithFormat:@"NONE reviews.user = %@", self.user]

// this works
[NSPredicate predicateWithFormat:@"SUBQUERY(reviews, $x, $x.user == %@).@count == 0", self.user];
like image 625
Duane Fields Avatar asked Jul 28 '11 23:07

Duane Fields


1 Answers

try this

NSPredicate *predicate = [NSCompoundPredicate notPredicateWithSubpredicate:[NSPredicate predicateWithFormat:@"ANY reviews.user = %@", self.user]];
like image 116
Gaurang Ratnaparkhi Avatar answered Oct 12 '22 23:10

Gaurang Ratnaparkhi