Skoota
Skoota

Reputation: 5290

Core Data many-to-many predicate

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.

Upvotes: 0

Views: 560

Answers (2)

Jordan Montel
Jordan Montel

Reputation: 8247

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

Upvotes: 1

Andy Etheridge
Andy Etheridge

Reputation: 1283

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.

Upvotes: 1

Related Questions