NSFetchedResultsController,指定返回实体的数量?

时间:2012-08-23 17:09:35

标签: iphone cocoa-touch uitableview core-data nsfetchedresultscontroller

创建NSFetchRequest时是否有办法指定要返回的实体数量。在下面的示例中,我只想要拍摄次数最多的前3个Zombie作品,我认为fetchBatchSize可能会这样做,但它会返回3个批次,直到您的所有Zombie作品都被检索到。有谁知道你会怎么做?

- (void)setupFetchedResultsController {

    // CREATE FETCH REQUEST
    NSFetchRequest *fRequest = [[NSFetchRequest alloc] initWithEntityName:@"Production"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains %@", @"Zombie"];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"shots" ascending:NO];
    [fRequest setPredicate:predicate];
    [fRequest setSortDescriptors:[NSArray arrayWithObject:descriptor]];
    [fRequest setFetchBatchSize:3];

    // FETCH RESULTS CONTROLLER
    NSManagedObjectContext *moc = [[self managedDocument] managedObjectContext];
    [self setFetchedResultsController:[[NSFetchedResultsController alloc] initWithFetchRequest:fRequest
                                                                          managedObjectContext:moc
                                                                            sectionNameKeyPath:nil
                                                                                cacheName:nil]];
    [[self fetchedResultsController] setDelegate:nil]; // NO UPDATES
}

1 个答案:

答案 0 :(得分:1)

使用NSFetchRequest的{​​{3}}属性。

摘自Apple文档: 获取限制指定请求在执行时应返回的最大对象数。