为什么NSFetchedResultsController在设置获取批处理大小时加载所有行? [英] Why is NSFetchedResultsController loading all rows when setting a fetch batch size?

查看:111
本文介绍了为什么NSFetchedResultsController在设置获取批处理大小时加载所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSFetchedResultsController对于抓取批量大小设置为5。我们注意到它的缓慢加载,所以我们启用SQLDebug。由于某种原因,它一次加载5行,但它在初始加载时对所有100行执行此操作。一旦加载,它似乎一次加载5行。

NSFetchedResultsController is set to 5 for fetch batch size. We notice it's slow loading, so we enable SQLDebug. For some reason, it's loading 5 rows at a time, but it's doing this for all 100 rows on initial load. Once it's loaded, it seems to load 5 rows at a time.

观看控制台时,我看到查询运行了20次;总共有100行。我希望这将运行1次与前5行,当你滚动,更多的行将加载。我也尝试改变抓取大小,它有相同的行为。

When watching the console, I see the query ran 20 times; there are a total of 100 rows. I would have expected this to run 1 time with the first 5 rows and as you scroll, more rows would be loaded. I also tried changing the fetch size and it has the same behavior.

获取结果控制器的设置如下。

The setup of the fetched results controller is below.

- (NSFetchedResultsController*) fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    [NSFetchedResultsController deleteCacheWithName:@"Root"];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Message" inManagedObjectContext:_context];

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"conversation = %@", conversation];

    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
                              initWithKey:@"timestamp" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:5];

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                        managedObjectContext:_context sectionNameKeyPath:nil 
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];

    return _fetchedResultsController;    

}


推荐答案

NSFetchedResultsController与tableview结合使用。表视图将要求NSFetchedResultsController获得所需的行数,以填充它认为需要显示的行,并为了性能原因而保留。通过保持批量大小人为地低,您使NSFetchedResultsController进行更多的往返到商店的回程。

We use NSFetchedResultsController in conjunction with a tableview. The tableview will ask the NSFetchedResultsController for as many rows as it needs to fill up the rows it thinks it will need to show and keep on hand for performance reasons. By keeping the batch size artificially low, you are causing the NSFetchedResultsController to make more round trips back to the store.

将它设置为100左右,看看你的性能喜欢。如果批处理大小为100时出现性能问题,则可能必须规范化核心数据模型。查看2010年核心数据的WWDC视频。它有一个关于优化核心数据和与NSFetchedResultsController相关的抓取的伟大视频。

Set it to 100 or so and see what your performance is like. If you are having performance issue with a batch size of 100, you may have to normalize your core data model. Check out the WWDC videos on Core Data from 2010. It has a great video on optimizing core data and the fetches associated with NSFetchedResultsController.

祝你好运

这篇关于为什么NSFetchedResultsController在设置获取批处理大小时加载所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆