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;
Without having code to play with it is difficult to be 100% but you should be able to:
[NSPredicate predicateWithFormat:@"%@ in groups", [self group]];
This is assuming that your fetch is trying to pull back Item entities.
Your going about this backwards. Since you appear to already have the group (I assume self.group
is a Group
object), you should just ask self.group for its items using self.group.items. Once you have the Item
objects related to a particular Group
object then you can sort them for display however you wish.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With