首页 文章

CoreData:错误:使用NSFetchedResultsController时出现严重的应用程序错误

提问于
浏览
1

我正在使用带有UICollectionView的NSFetchedResultsController . 每当我从服务器接到电话时,我都会更新Core数据表,并使用核心数据通知更新UICollectionView . 当我这样做时,我收到这样的错误,

CoreData:错误:严重的应用程序错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . 更新无效:第0部分中的项目数无效 . 更新后现有部分中包含的项目数(12)必须等于更新前该部分中包含的项目数(11),加上或减去数字从该部分插入或删除的项目(0已插入,0已删除)和加或减移入或移出该部分的项目数量(0移入,0移出) . 用户信息(null)

来自这个帖子Assertion failure when i use the Add Function

我知道我的Collectionview正在进行更新时正在更新 . 我的问题是,我该如何防止这种情况?

我的代码:

(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {@synchronized(self){if(controller == myFetchedResultsController){if([self.sectionChangesInCollectionView count]> 0){[self.myCollectionView performBatchUpdates:^ {for(NSDictionary *更改self.sectionChangesInCollectionView)
{
[更改enumerateKeysAndObjectsUsingBlock:^(NSNumber * key,id obj,BOOL * stop)
{NSFetchedResultsChangeType type = [key unsignedIntegerValue];
开关(类型)
{
case NSFetchedResultsChangeInsert:
[self.myCollectionView insertSections:[NSIndexSet indexSetWithIndex:[obj
unsignedIntegerValue]]];打破; case NSFetchedResultsChangeDelete:[self.myCollectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];打破; case NSFetchedResultsChangeUpdate:[self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];打破; }};完成:nil]; } if([self.objectChangesInCollectioView count]> 0 && [self.sectionChangesInCollectionView count] == 0)
{

if([self shouldReloadCollectionViewToPreventKnownIssue] ||
self.myCollectionView.window == nil){//这是为了防止UICollectionView中出现错误 . //在插入第一个对象或删除集合视图中的最后一个对象时,该错误会自动出现 . // UICollectionView断言失败//一旦修复了bug,就应该删除这段代码,在OpenRadar // http://openradar.appspot.com/12954582 [self.myCollectionView reloadData]中跟踪它; }
其他
{

[self.myCollectionView performBatchUpdates:^ {

for(NSDictionary *在self.objectChangesInCollectioView中更改)
{
[更改enumerateKeysAndObjectsUsingBlock:^(NSNumber * key,id obj,BOOL * stop)
{NSFetchedResultsChangeType type = [key unsignedIntegerValue];
开关(类型)
{
case NSFetchedResultsChangeInsert:
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.myCollectionView insertItemsAtIndexPaths:@ [obj]];
});
打破;
}
case NSFetchedResultsChangeDelete:
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.myCollectionView deleteItemsAtIndexPaths:@ [obj]];
});
打破;
}
case NSFetchedResultsChangeUpdate:
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.myCollectionView reloadItemsAtIndexPaths:@ [obj]];
});
打破;
}
case NSFetchedResultsChangeMove:
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.myCollectionView moveItemAtIndexPath:obj [0]
toIndexPath:OBJ1]; });打破; }}};完成:nil]; } [self.sectionChangesInCollectionView removeAllObjects];
[self.objectChangesInCollectioView removeAllObjects];
}
其他
{

}
pragma mark - collectionView bug fix method(BOOL)shouldReloadCollectionViewToPreventKnownIssue {__block BOOL shouldReload = NO; for(NSDictionary * self in self.objectChangesInCollectioView){[change enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL * stop){NSFetchedResultsChangeType type = [key unsignedIntegerValue]; NSIndexPath * indexPath = obj; switch(type){case NSFetchedResultsChangeInsert:if([self.myCollectionView numberOfItemsInSection:indexPath.section] == 0) else {shouldReload = NO;打破case NSFetchedResultsChangeDelete:if([self.myCollectionView numberOfItemsInSection:indexPath.section] == 1) else {shouldReload = NO;打破case NSFetchedResultsChangeUpdate:shouldReload = NO;打破; case NSFetchedResultsChangeMove:shouldReload = NO;打破; }}; } return shouldReload; }

1 回答

  • 0

    删除核心数据项,让委托回调负责删除表视图行 . 看看this回答 .

相关问题