Skoota
Skoota

Reputation: 5290

Core Data many-to-many predicate (again)

Yesterday I asked a question about a many-to-many relationship:

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.

...and received some very helpful advice. I would now like to ask a different, but related, question:

I would like an NSPredicate that will return all Category objects NOT associated with a given Database object.

I require this as a predicate since it is being used as part of a fetched results controller.

Any help greatly appreciated. Thanks in advance!

Upvotes: 1

Views: 167

Answers (2)

Martin R
Martin R

Reputation: 540065

It seems to be a Core Data bug that "NOT ANY" queries do not work correctly (see Core Data NSPredicate with to-Many Relationship for a similar issue).

As a workaround, you can use the following predicate with "SUBQUERY":

[NSPredicate predicateWithFormat:@"SUBQUERY(databases, $db, $db == %@).@count == 0", theDatabase]

Upvotes: 2

Sviatoslav Yakymiv
Sviatoslav Yakymiv

Reputation: 7935

Try to use:

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

Or

[NSPredicate predicateWithFormat:@"NOT(%@ IN databases)", database]

Upvotes: 0

Related Questions