0

I have two entities with "to-many" relation

enter image description here

Now I want to fetch all Categories which has ONLY favorite (isFavorite) Recipes.

This is like:

SELECT c.*  
FROM zcategories c, zrecipes r
where 
    r.zisfavorite = 1
    and r.zincategory = c.z_pk
group by c.zname;

I tried

    - (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    self.checkedMOC = appDelegate.managedObjectContext;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:self.checkedMOC];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL recipes.isFavorite == %@", [NSNumber numberWithInt:1]];
    [fetchRequest setEntity:entity];
    [fetchRequest setPredicate:predicate];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.checkedMOC sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
}  

but the error occurs 'to-many key not allowed here'

Romowski
  • 1,518
  • 5
  • 25
  • 50

1 Answers1

1

If you want the category instances that have one or more recipes that are favorites, try the ANY keyword

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY recipes.isFavorite == %@", [NSNumber numberWithInt:1]];

If you want the category instances which have only recipes that are favorites, you can use the ALL keyword:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL recipes.isFavorite == %@", [NSNumber numberWithInt:1]];
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
  • Errors: 1. **The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'** 2. **'Unsupported predicate (null)'** – Romowski Feb 27 '13 at 04:28
  • You should post more of your code, not just the line where you've created the NSPredicate. It is likely that your problems stem from something other than just the predicate creation. – Tim Dean Feb 27 '13 at 04:31
  • There might also be a problem with the use of ALL directly against a SQLite persistence store that you need to work around. Documentation and reality don't seem to match up here. See the answers to http://stackoverflow.com/questions/5922139/crash-using-aggregate-operation-all-in-a-core-data-ios-application for more info – Tim Dean Feb 27 '13 at 04:35
  • Error 1 could be caused by attempting to reuse the same cache name ("Master") with multiple NSFetchedResultsController instances. You can't share the same cache name across different instantiations of the controller. – Tim Dean Feb 27 '13 at 04:48
  • I have changed cache name and then the fetch works! Thank you ) – Romowski Feb 27 '13 at 04:57