How to configure an NSPredicate that filters items in a specified group using NSFetchedResultsController

I have a Group object that has many Item objects. An Item object can be in several groups. The relevant part of my model looks like this:

alt text

Problem

When i call

[fetchedResultsController performFetch:&error]

I get this error in console

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' ***

I am sure the problem is how I create the predicate here:

[NSPredicate predicateWithFormat:@"groups == %@", self.group];

But I can’t say what I am doing wrong. I read the NSPredicate docs and I 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 them work. It should be simple, but I can't figure it out. I just want the predicate to filter out all the elements in order to return only those elements that are members (through the model relation) of the group. How do you do this?

NSFetchedResultsController, . NSFetchedResultsController :

// 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;
+5
2

100%, :

[NSPredicate predicateWithFormat:@"%@ in groups", [self group]];

, .

+6

. ( , self.group Group), self.group , self.group.items. Item, Group, , .

+1

All Articles