I have a Group entity which has many Item entities. An Item entity can be in multiple groups. The relevant portion of my model looks like this:
The Problem
When I call
[fetchedResultsController performFetch:&error]
I get this error in the console
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' ***
I'm pretty sure the problem is in how I'm setting up the predicate here:
[NSPredicate predicateWithFormat:@"groups == %@", self.group];
But I can't tell what I'm doing wrong. I've read over the NSPredicate documents and I've tried this:
[NSPredicate predicateWithFormat:@"ANY groups IN %@", [NSArray arrayWithObject:self.group]];
[NSPredicate predicateWithFormat:@"ANY groups LIKE %@", self.group];
[NSPredicate predicateWithFormat:@"ANY groups == %@", self.group];
[NSPredicate predicateWithFormat:@"ANY groups IN %@", [NSArray arrayWithObject:self.group]];
[NSPredicate predicateWithFormat:@"groups == %@", self.group];
None of which work. This has got to be simple, but I can't figure it out. I simply want the predicate to filter all the items so as to only return items that are members (through the model relationship) of the group. How do you do this?
I have a NSFetchedResultsController that I am trying to configure to show only the items in a specific group. My code to setup the NSFetchedResultsController looks like this:
// create the fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// configure the entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// configure the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"groups == %@", self.group];
[fetchRequest setPredicate:predicate];
// configure the sort descriptors
NSSortDescriptor *indexSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"uniqueID" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:indexSortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// create the fetched results controller
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"ItemsCache"];
// configure the fetched results controller
aFetchedResultsController.delegate = self;