Reputation: 265
I am banging my head on this one... My app has the following coredata entities:
MenuItem ContentItem
MenuItems can have many ContentItems and each ContentItem can belong in many MenuItems, so I have a many to many relationship between the two.
The relationships are: MenuItem -->> ContentItem: menuContentItems
ContentItem -->> MenuItem: contentItemMenuItem
I am trying to use the MenuItem's title attribute for the predicate, so my selected MenuItem's title is used in the predicate.
When the user selects a MenuItem, I want to display the ContentItems related to that MenuItem.
To show the correct list of ContentItems, I tried a standard predicate like this:
[fetchedRequest setPredicate:[NSPredicate predicateWithFormat:@"ANY contentItemMenuItem.title = %@", selectedMenuItem.title]];
No luck. I get this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath contentMenuItem.title not found in entity <NSSQLEntity ContentItem id=2>'
I also tried a subquery like this:
[fetchedRequest setPredicate:[NSPredicate predicateWithFormat:@"SUBQUERY(MenuItem, m$, m.title = %@)",selectedMenuItem.menuItemTitle]];
Also no luck. Error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "SUBQUERY(MenuItem, m$, m.title = %@)"'
There has to be a way for this to work.
Upvotes: 0
Views: 316
Reputation: 38728
You should be able to just use
[selectedMenuItem valueForKey:@"menuContentItems"];
Out of curiosity if you ever needed to get that SUBQUERY
working it would look like this
This evaluates to a collection e.g. `contentItem.menuItems`
|
| This is the predicate
| |
v V
SUBQUERY(menuItems, $menuItem, $menuItem.title == 'test').@count > 0
^
|
This is what each item will be referred as in the predicate
The subquery will return a collection of results so we then just ask if there are more than 0 items returned, if so then we had a match.
Upvotes: 4