Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...