如何使用fetchedResultsController只返回某些实体?

时间:2012-08-21 05:17:40

标签: ios xcode core-data

现在我程序中的setupFetchedResultsController返回“Food”类型的所有实体,但我只想要“Food”类型的实体,它们也是我当前正在显示的“List”实体。

holdBy定义了“食物”和“列表”之间的关系

有办法做到这一点吗?

我的setupFetchedResultsController

- (void)setupFetchedResultsController
{
    // 1 - Decide what Entity you want
    NSString *entityName = @"Food"; // Put your entity name here
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    // 2 - Request that Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

    // 3 - Filter it if you want
    //request.predicate = [NSPredicate predicateWithFormat:@"Role.name = Blah"];

    // 4 - Sort it if you want
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
                      ascending:YES
                      selector:@selector(localizedCaseInsensitiveCompare:)]];
    // 5 - Fetch it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                      managedObjectContext:self.managedObjectContext
                      sectionNameKeyPath:nil
                      cacheName:nil];
    [self performFetch];
}

1 个答案:

答案 0 :(得分:0)

我认为你可以使用你注释掉的谓词。

request.predicate = [NSPredicate predicateWithFormat:@"heldBy.name = name_of_list"];