0

「対多」関係を持つ2つのエンティティがあります

ここに画像の説明を入力

ここで、お気に入り(isFavorite) のレシピのみを含むすべてのカテゴリを取得したいと考えています。

これは次のようなものです:

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

私は試した

    - (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;
}  

しかし、 「to-many key not allowed here」というエラーが発生します

4

1 に答える 1

1

お気に入りのレシピが 1 つ以上あるカテゴリ インスタンスが必要な場合は、次のANYキーワードを試してください。

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

お気に入りのレシピのみを含むカテゴリ インスタンスが必要な場合は、次のALLキーワードを使用できます。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL recipes.isFavorite == %@", [NSNumber numberWithInt:1]];
于 2013-02-27T04:21:18.830 に答える