Core Data many-to-many predicate

北城余情 提交于 2019-12-12 01:56:14

问题


I have two entities which are related by a many-to-many relationship:

Database <<----->> Category

In other words, a database can have many categories and a category can be associated with many databases.

I need an NSPredicate that will return all Category objects associated with a given database object. Any help would be appreciated.


回答1:


You don't need a predicate. Given that you have a relationship called categories that is the to-many relationship to your Category entities, then

NSSet *categoriesForDatabase = database.categories;

If you really want to use a predicate then it would be:

[NSPredicate predicateWithFormat:@"ANY databases = %@", database];

where "databases" is the name of the to many relationship on the Category entity and database is an instance of a Database entity.




回答2:


You want to compare a collection (all Category objects) to a given object, you can try something like this :

[NSPredicate predicateWithFormat:@"ANY categories = %@", category];

or

[NSPredicate predicateWithFormat:@"ANY databases = %@", database];


来源:https://stackoverflow.com/questions/19155743/core-data-many-to-many-predicate

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