Core Data predicate : unimplemented SQL generation for predicate
This exception is also raised if one of the predicates uses a column (i.e. field) name that does not exist. Totally misleading error message...
"ANY" in a Core Data predicate works only for a single to-many relationship. Since your query involves two to-many relationships, you have to use a SUBQUERY:
[NSPredicate predicateWithFormat:@"SUBQUERY(models, $m, ANY $m.trims IN %@).@count > 0",
arrayOfTrims];
The keyword IN
can be used but you cannot apply ANY at the same time as that does not make sense when you turn it into SQL.
The predicate you are most likely looking for is:
[NSPredicate predicateWithFormat:@"models.trims IN %@", arrayOfTrims];
But that isn't going to work in this case either because you are going across a relationship. So what you need to do is reverse the whole thing:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Model"];
[request setPredicate:[NSPredicate predicateWithFormat:@"trims in %@", arrayOfTrims]];
NSError *error = nil;
NSArray *modelArray = [moc executeFetchRequest:request error:&error];
if (!modelArray) {
NSLog(@"Error: %@\n%@", [error localizedDescription], [error userInfo]);
}
NSArray parentObjectArray = [modelArray valueForKey:@"${PARENT_RELATIONSHIP_NAME}"];
Basically you are fetching the child objects to satisfy your ANY
and then using KVC to retrieve the parent objects that you care about.
When you use a predicate in a CoreData operation, the predicate gets translated into SQL. That translation is not possible for all NSPredicate operations, you've hit one that isn't. My suggestion would be something along the lines of:
NSMutableArray* predicates = [NSMutableArray new];
for(NSString* trim in arrayOfTrims)
{
[predicates addObject:[NSPredicate predicateWithFormat:@"%@ IN models.trims", trim]];
}
NSPredicate* predicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates];