Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I set up an NSPredicate that filters for items in a specified group using NSFetchedResultsController
    primarykey
    data
    text
    <p>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:</p> <p><img src="https://dl.dropbox.com/u/2523010/CoreDataQuestion.png" alt="alt text"></p> <p><strong>The Problem</strong></p> <p>When I call</p> <pre><code>[fetchedResultsController performFetch:&amp;error] </code></pre> <p>I get this error in the console</p> <pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' *** </code></pre> <p>I'm pretty sure the problem is in how I'm setting up the predicate here:</p> <pre><code>[NSPredicate predicateWithFormat:@"groups == %@", self.group]; </code></pre> <p>But I can't tell what I'm doing wrong. I've read over the NSPredicate documents and I've tried this:</p> <pre><code>[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]; </code></pre> <p>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?</p> <p>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:</p> <pre><code>// 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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload