为什么我的所有NSManagedObjects立即出错? [英] Why are all my NSManagedObjects being faulted immediately?

查看:110
本文介绍了为什么我的所有NSManagedObjects立即出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行一个非常基本的获取请求,返回大约2000个对象。我使用一个batchSize为15的NSFetchedResultsController。

I'm running a very basic fetch request that returns about 2000 objects. I'm using an NSFetchedResultsController with a batchSize of 15.

    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:15];
   self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:@"createDay" cacheName:nil];

获取请求需要10秒以上。我已经启用SQLite调试,所以我可以看到发生了什么。我认为它正在获取所有2000个实体,只有15与实际值,然后由于某种原因经过每一个2000年的对象和错误他们。

The fetch request takes over 10 seconds though. I've enabled SQLite debugging so I can see what's going on. I think it's fetching all 2000 entities, with only 15 with actual values, then for some reason going through each and every of the 2000 objects and faulting them in.

fetch,这些行出现数千次:

During the fetch, these lines appear thousands of times:

2012-06-22 21:14:47.546 app[9227:707] CoreData: annotation: sql connection fetch time: 0.0107s
2012-06-22 21:14:47.551 app[9227:707] CoreData: annotation: total fetch execution time: 0.0171s for 15 rows.
2012-06-22 21:14:47.568 app[9227:707] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZAUTHOREMAIL, t0.ZAUTHORNAME, t0.ZCREATEDATE, t0.ZISGLOBAL, t0.ZISLOCKED, t0.ZISNEW, t0.ZISPENDINGDELETE, t0.ZISPENDINGSYNC, t0.ZLASTUPDATED, t0.ZLOCALLYMODIFIEDDATE, t0.ZMETALASTUPDATED, t0.ZNOTEID, t0.ZNUMBEROFCHILDREN, t0.ZPARENTAUTHOREMAIL, t0.ZPARENTNOTEID, t0.ZROOTAUTHOREMAIL, t0.ZROOTNOTEID, t0.Z4PENDINGADDNOTES, t0.Z4PENDINGREMOVENOTES FROM ZMBNOTEOBJECT t0 WHERE  t0.Z_PK IN  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)  ORDER BY t0.ZCREATEDATE DESC LIMIT 15
...thousands more lines exactly similar to the three above

我的表只显示5个单元格一次,所以我不知道为什么所有的对象都被立即错误。可能是什么原因?为什么所有的对象都会立即出错,没有我甚至滚动我的表?它们可能是被访问的地方,或许是迭代?并调用的NSArray * fetchedObjects = fetchedResultsController.fetchedObjects 造成任何机会有故障的所有对象?

My table only shows 5 cells a time, so I have no idea why all the objects are being faulted in immediately. What could be causing this? Why would all the objects be faulted in immediately without me even scrolling my table? Can it be that they are being accessed somewhere, perhaps iterated? Does calling NSArray *fetchedObjects = fetchedResultsController.fetchedObjectscause all objects to be faulted by any chance?

推荐答案

查看它在 batchSize (我强调)的文档中说的:

Check out what it says in the documentation about batchSize (my emphasis):


默认值为0.批量大小为0被视为无穷大,这会禁用批处理错误行为。

The default value is 0. A batch size of 0 is treated as infinite, which disables the batch faulting behavior.

-zero批量大小,执行提取时返回的对象集合被分成批。执行提取时,整个请求被评估和记录所有匹配对象的标识,但不超过 batchSize objects'将一次从持久存储区中提取数据。从执行请求返回的数组将是一个代理对象,它根据需要透明地对批处理进行故障排除。 (在数据库术语中,这是一个内存中光标。)

If you set a non-zero batch size, the collection of objects returned when the fetch is executed is broken into batches. When the fetch is executed, the entire request is evaluated and the identities of all matching objects recorded, but no more than batchSize objects’ data will be fetched from the persistent store at a time. The array returned from executing the request will be a proxy object that transparently faults batches on demand. (In database terms, this is an in-memory cursor.)

您可以使用此功能来限制应用程序中的工作集数据。在与fetchLimit组合,您可以创建任意结果集的子区间。

You can use this feature to restrict the working set of data in your application. In combination with fetchLimit, you can create a subrange of an arbitrary result set.

所以,很显然,按照这样的解释,你会得到2000/15往返商店。这肯定需要一些时间。你需要所有的数据,因为你的排序和 ANY 谓词,但你指示请求一次只得到15。

So, clearly, according to this explanation, you get 2000/15 round trips to the store. That will certainly take some time. You need all the data because of your sort and the ANYpredicate but you instructed the request to only get 15 at a time.

另外,为了完整性,从前面的注释:从你的排序描述符中删除比较

Also, for completeness, from the previous comments: drop the compare from your sort descriptor.

这篇关于为什么我的所有NSManagedObjects立即出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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