CoreData - NSPredicate with NSSet

南楼画角 提交于 2019-12-21 22:24:54

问题


I have a DB with the following relationships:

A <<-->> B

A: Members table; B: Activities table

A Member has many Activities, and each Activity has many Members. So, a many to many relationship.

When the user selects an Activity from a tableView, a new tableView should be pushed with all the Members that has that Activity.

In the NSPredicate of the "child" tableView, I do:

request.entity = [NSEntityDescription entityForName:@"Members" inManagedObjectContext:context];    
request.predicate = [NSPredicate predicateWithFormat:@"memberActivity = %@", [NSSet setWithObject:activity]];

And the result is:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

What am I doing wrong?

Thanks,

RL


回答1:


Try

request.predicate = [NSPredicate predicateWithFormat:@"self in %@", [activity hasMembers]];

I haven't done much of this complexity with CoreData, but my understanding is that query should return all the Member objects that are in the hasMembers set, using the managed object ID maintained by CoreData. Let me know how it goes.



来源:https://stackoverflow.com/questions/7235790/coredata-nspredicate-with-nsset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!