Ticko Ticko - 1 year ago 187
Objective-C Question

Core Data predicate : unimplemented SQL generation for predicate

Basically I got 3 entities in my data model : Brand, Model and Trim.

  • A brand has a one-to-many relationship with Model called "models". (one brand have multiple models but a model has only one brand)

  • A model has a many-to-many relationship with Trim called "trims". (a model can have multiple trims, and a trim can have multiple models)

Having an array of trims objects, I would like to get all the brands having a model which "contains" at least one trim contained inside this array.

So here is my predicate for the fetch request :

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Brand"];
[NSPredicate predicateWithFormat:@"ANY models.trims IN %@", arrayOfTrims];

And here is the error I'm getting :

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (ANY models.trims IN {<Trim: 0x8e60340> (entity: Trim; id: 0x8e62f10 <x-coredata://BCD28560-AED5-4409-9A35-1925001773E6/Trim/p8>

I'm kinda new to Core Data and I have no idea what I'm doing wrong.

Hope someone can help,

Thanks a lot.

Answer Source

"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",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download