I have the following model graph:
+-----------+ +-----------+
| Container | | Group |
+-----------+ +-----------+
| groups |<-->>| container |
+-----------+ +-----------+
^ ^
| |
+-----------+ +-----------+ +-----------+ +---------+
| Owner | | ToyBox | | ToyType | | Item |
+-----------+ +-----------+ +-----------+ +---------+
| toyBox |<--->| owner | | items |<-->>| toyType |
+-----------+ +-----------+ +-----------+ +---------+
In a UITableView I am to display a list a list of Items. In this case I only want to show the items that belong to a particular owner. To do this I will use NSFetchedResultsController to display the items. This means that I need create an NSFetchRequest with an appropriate NSPredicate to give to the NSFetchedResultsController.
Attempting to use a keypath predicate causes an exception due to the parent entities. This appears to be an Apple bug or decision not to support. A radar has been filed. Additionally, I do not wish to flatten the entities.
So that left me with an attempt to do this with SUBQUERY() as follows:
NSFetchRequest *itemsFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
NSPredicate *itemsPredicate = [NSPredicate predicateWithFormat:@"SUBQUERY(toyItem, $g, SUBQUERY($g.container, $c, SUBQUERY($c.owner, $o, $o = %@).@count > 0).@count > 0).@count > 0", ownerObject];
This results in the following exception:
Can't have a non-relationship collection element in a subquerySUBQUERY($c.owner, $o, $o == <MLMOOwner: ...
I realize that because the relationship is one to one between Owner and ToyBox that there isn't a collection returned and this is the problem. So my questions are:
1) Is there a way to force the return of a collection for to-one relationships in the subquery?
2) If not is there another way to go about creating this predicate for the fetch request?