【问题标题】:NSFetchedResultsController – getting exception when a new section is created (UPDATE: might be when all rows in a section is deleted...)NSFetchedResultsController – 创建新部分时出现异常(更新:可能是在删除部分中的所有行时......)
【发布时间】: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


    【解决方案1】:

    我想我解决了。问题是在确定是否删除行/节时必须考虑一些额外的条件。使用 Apple 文档中提供的代码(以及一些调整以使其在我看来可以正常工作)时,它运行正常。

    2011-03-22 更新

    基本上我使用了与SafeFetchedResultsController中相同的方法。

    【讨论】:

    • 解释一下你做了什么并解决了你的问题可能是个好主意。
    • 在上面添加了评论和链接。
    • 文件不在链接中。有人可以分享它或提供另一个与 SafeFetchedResultsController 的链接吗?
    【解决方案2】:

    我不知道这是否是您的错误的原因,但您在委托方法中缺少 NSFetchedResultsChangeMove 案例。

    case NSFetchedResultsChangeMove:
        [changedTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [changedTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
        break;
    

    【讨论】:

    • 感谢您的建议!但是,即使无法移动对象,我也需要它吗?只能插入新的,删除旧的(根据后台的JSON导入),但用户不能对行重新排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2011-04-24
    • 1970-01-01
    相关资源
    最近更新 更多