views:

73

answers:

2

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:

alt text

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;
+1  A: 

Your going about this backwards. Since you appear to already have the group (I assume self.group is a Groupobject), 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.

TechZen
I get that, and if I was on the Mac, that's exactly what I'd do, but I'm on the iPhone and the NSFetchedResultsController provides some real advantages there, so I'd like to use it. That's the the core of the question: How do I use NSFetchedResultsController given this model setup.
David Weiss
+1  A: 

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.

Marcus S. Zarra
Awesome! That was it! Thanks Marcus. It makes perfect sense now that I see it.
David Weiss