iPhone4 iOS5 NSFetchedResultsController如何挑选批量大小?

时间:2011-11-12 03:02:19

标签: objective-c core-data ios5 iphone-4 nsfetchedresultscontroller

我现在有5个获取的结果控制器,我正在添加我的第一个实际使用数据,而不是仅显示它。我希望控制器最多可以管理150个对象。我应该选择哪种批量大小,一次最多可以处理5个对象?是5吗?

  - (NSFetchedResultsController *)estimatorEventsController
    {
        if (__estimatorEventsController != nil)
        {
            return __estimatorEventsController;
        }

        /*
         Set up the fetched results controller.
         */
        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"EstimatorEvent" inManagedObjectContext:self.managedObjectContext];

        [fetchRequest setEntity:entity];

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

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"epoch" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: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.managedObjectContext sectionNameKeyPath:nil cacheName:@"EstimatorEvents"];
        aFetchedResultsController.delegate = self;
        self.estimatorEventsController = aFetchedResultsController;


        NSError *error = nil;
        if (![self.estimatorEventsController performFetch:&error])
        {

            NSLog(@"Unresolved error __remindersController %@, %@", error, [error userInfo]);
            //      abort();
        }

        return __estimatorEventsController;
    }    

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

亚历,

我在获取的结果控制器中有800多个项目,并且不批量提取。不要担心150件......你知道,过早的优化和所有这些。

要记住的另一件事是批量限制是在获取请求而不是获取的结果控制器上。这种区别很重要,因为获取请求是一个较低级别的项目,可以轻松触发包含数千个项目的结果数组。必须小心这些大小的查询。 150行并不是什么大不了的事。

安德鲁

答案 1 :(得分:1)

我没有非常明智的答案,但似乎大多数指南似乎都建议你加载的电池大约是屏幕上出现的电池数量的两到三倍,所以如果你有表中一次可见五个,那么大概15个?它是不同时加载所有内容的好处之间的平衡,而不是因为它们有一些负载和延迟而没有太多的获取请求,并且确保您已经有一些尚未在屏幕上预取的单元格在突然间响应快速滚动。