CoreData:Section中的numberOfObjects错误 [英] CoreData: numberOfObjects in Section incorrect

查看:60
本文介绍了CoreData:Section中的numberOfObjects错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS应用中使用CoreData来保存位置对象(名称,坐标)的简单列表。在内存警告之后,当CoreData记录似乎已损坏时,我现在遇到了两次情况。

I'm using CoreData in an iOS app to save a simple list of location objects (name, coordinates). I've run into a situation twice now, after a memory warning, when the CoreData records appear to become corrupt.

具体来说,(在tableView:numberOfRowsInSection:中)

Specifically, (in tableView:numberOfRowsInSection:)

NSUInteger numObjects = [[[fetchedResultsController sections] objectAtIndex:section] numberOfObjects];

返回== 0的4。

然后(在tableView:cellForRowAtIndexPath:中)

Then (in tableView:cellForRowAtIndexPath:)

aLocation = [fetchedResultsController objectAtIndexPath:indexPath];

对于indexPath [0,3]会产生异常:

for indexPath [0,3] generates an exception:

NSRangeException: * -[_ PFArray objectAtIndex:]:索引(3)超出范围(3)

NSRangeException: * -[_PFArray objectAtIndex:]: index (3) beyond bounds (3)

ie CoreData表示一个表中有4行,但是当表视图请求该对象时,最后一行就无法返回对象。

i.e. CoreData says there are 4 rows in a table, but then can't return an object for the last row when the table view requests it.

两个问题:

1)关于这种情况如何发生以及如何预防的任何想法

1) Any thoughts on how this might arise and how to prevent it

2)一旦发生,是否存在有什么办法解决代码中的数据问题?

2) Once it has occurred, is there any means to fix the data problem in code?

谢谢

Stephen

推荐答案

该问题似乎是导致CoreData高速缓存出现某些不一致的原因-不清楚是什么原因导致的,因为谓词从未更改。如果访问fetchedResultsController中的最后一个对象时引发异常,我最终会调用deleteCacheWithName。这似乎已经解决了该问题(6个月内用户没有问题)。

The issue appears to have been some inconsistency in the CoreData cache that was causing this - unclear what causes it, as the predicates are never changed. I ended up calling deleteCacheWithName if an exception is ever raised on accessing the last object in the fetchedResultsController. This appears to have resolved the issue (no issues from users in 6 months on this).

这篇关于CoreData:Section中的numberOfObjects错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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