Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up an NSPredicate that filters for items in a specified group using NSFetchedResultsController

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;
like image 822
David Weiss Avatar asked Aug 05 '10 20:08

David Weiss


2 Answers

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.

like image 64
Marcus S. Zarra Avatar answered Oct 03 '22 10:10

Marcus S. Zarra


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.

like image 33
TechZen Avatar answered Oct 03 '22 10:10

TechZen