批处理核心数据提取结果 [英] Batching Core Data fetch results

查看:142
本文介绍了批处理核心数据提取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个获取请求,返回大约2000个实体。目前,我的设备大约需要20秒。所以我想我可能设置一个抓取限制为100,然后当用户滚动到表视图的结尾,获取下100个实体。这可以使用 NSFetchRequest的 setFetchLimit setFetchOffset 来完成。



然而,我不能想出的是,如果在我的第二次抓取,我正在抓取对象101-200,对象会发生什么1-100 ?我必须为每100个项目使用单独的 NSFetchedResultsController ,然后配置我的表视图数据源方法基于多个获取结果控制器查询?或者我可以以某种方式使用相同的 NSFetchedResultsController 以某种方式一次提取100个实体,但是每次后续提取,只是添加下100个项目到原始的100个项目? p>

编辑:这是我的代码:

  NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; 

NSEntityDescription * entity = [NSEntityDescription
entityForName:@MessageObjectinManagedObjectContext:appDelegate.managedObjectContext];

[fetchRequest setEntity:entity];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY tags.tagName ==%@,currentTagObject.tagName];
[fetchRequest setPredicate:predicate];
NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@createDateascending:NO selector:@selector(compare :)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:5];


解决方案

你实际上不需要做任何事情以您指定的大小批量提取。您将获得您描述的行为只是在单个控制器上执行单个获取请求 - 通过设置批量大小属性,您只是暗示CoreData您认为是您自己考虑的最佳批处理大小。 CoreData将按照您需要的大小顺序处理提取对象,并且在内存消耗的必要时会故障管理对象。


I'm executing a fetch request that returns about 2000 entities. Currently, this takes about 20 seconds on my device. So I thought I might set a fetch limit of 100, and then when the user scrolls to the end of the table view, fetch the next 100 entities. This can be accomplished using NSFetchRequest's setFetchLimit and setFetchOffset.

However, what I can't figure out is, if on my second fetch where I'm fetching objects 101-200, what would happen to the objects 1-100? Would I have to use separate NSFetchedResultsController for every 100 items, and then configure my table view data source methods to query based on multiple fetch results controllers? Or can I somehow use the same NSFetchedResultsController to somehow fetch 100 entities at a time, but upon every subsequent fetch, just add the next 100 items to the original 100 items?

Edit: Here's my code:

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"MessageObject" inManagedObjectContext:appDelegate.managedObjectContext];

    [fetchRequest setEntity:entity];
    NSPredicate *predicate= [NSPredicate predicateWithFormat:@"ANY tags.tagName==%@", currentTagObject.tagName];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *sort= [[NSSortDescriptor alloc] initWithKey:@"createDate" ascending:NO selector:@selector(compare:)];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:5];

解决方案

You don't actually need to do anything to achieve the fetching in batches of the size you've specified. You will get the behavior you're describing simply be executing a single fetch request on a single controller - by setting the batch size property, you're simply hinting to CoreData what you consider to be the optimal batch size by your own consideration. CoreData will handle fetching objects in sequential batches of the size specified as you need them - and it will fault managed objects when necessary for memory consumption.

这篇关于批处理核心数据提取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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