【发布时间】:2011-01-31 20:07:07
【问题描述】:
我有一个带有 TableView 和 NSFetchedResultsController 的视图。 我正在使用 ASINetworkQueue(NSOperationQueue 的子类)和 ASIHTTPRequest 的子类(又是 NSOperation 的子类)来下载 JSON 提要,解析它并将相应的实体插入核心数据。 因此,在 ASIHTTPRequest 子类中,我有第二个 NSManagedObjectContext 来保持一切线程安全和美观。
一切都很好,我的后台获取/导入每 10 秒左右触发一次,新实体被创建并保存到核心数据存储中。 NSNotification 传播到 ViewController 和 NSFetchedResultsController 并且新行出现在 TableView 中。
当 JSON 包含具有新的 section 键值的实体(我们称之为“sectionID”)时,就会出现问题 - 例如 sectionID == 2 而不是 sectionID == 1(你明白吗?)。
此时,NSFetchedResultsController 应该让表格视图创建一个新部分,但我得到了一个异常:
Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** -[NSArray initWithObjects:count:]: attempt to insert nil object at objects[0] with userInfo (null)
Assertion failed: (_Unwind_SjLj_Resume() can't return), function _Unwind_SjLj_Resume, file /SourceCache/libunwind/libunwind-24.1/src/Unwind-sjlj.c, line 326.
这是我的 NSFetchedResultControllers 委托方法的代码:
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[[self eventTable] beginUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
NSIndexSet* set = [NSIndexSet indexSetWithIndex:sectionIndex];
switch (type) {
case NSFetchedResultsChangeInsert:
[[self eventTable] insertSections:set withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[[self eventTable] deleteSections:set withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView* tv = [self eventTable];
switch (type) {
case NSFetchedResultsChangeInsert:
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self tableView:tv configureCell:[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[[self eventTable] endUpdates];
}
对导致异常的原因有什么想法吗?提前致谢!
2011-02-03 更新 不确定是在创建新部分还是删除旧部分时发生错误。我几乎认为当一个部分中的所有行都被删除时会发生这种情况,但由于某种原因,控制器:didChangeSection:atIndex:forChangeType 没有被调用。 有没有类似经历的人?
2011-02-08 更新 我想我解决了。问题是在确定是否删除行/节时必须考虑一些额外的条件。 使用 Apple 文档中提供的代码(以及一些调整以使其在我看来可以正常工作)时,它运行正常。
【问题讨论】:
标签: iphone uitableview core-data nsfetchedresultscontroller nsoperation