【问题标题】:NSFetchedResultsController:objectAtIndexPath: causes NSInvalidArgumentExceptionNSFetchedResultsController:objectAtIndexPath: 导致 NSInvalidArgumentException
【发布时间】:2015-09-18 12:28:50
【问题描述】:

我正在使用一些类来处理 Core Data 和远程 JSON 服务之间的同步,并且在其中一个类中管理当前在 Core Data 中的对象,我遇到了几次随机崩溃。

方法

 @interface XLLocalDataLoader() <NSFetchedResultsControllerDelegate>

    // private properties
    @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;

方法

// get core data object at index path
-(NSManagedObject *)objectAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] < [self numberOfRowsInSection:indexPath.section]){
        return [self.fetchedResultsController objectAtIndexPath:indexPath];
    }
    return nil;
}

源代码

源代码位于GitHub -> XLDataLoader -> XLLocalDataLoader

错误

致命异常:NSInvalidArgumentException 对象的持久存储 无法从此 NSManagedObjectContext 的协调器访问

已调查

  1. NSFetchedResultsController crashes on objectAtIndexPath?
  2. NSFetchedResultsController objectAtIndexPath crash (EXC_BAD_ACCESS)
  3. NSFetchedResultsController Crash
  4. NSFetchedResultsController objectAtIndex, objectAtIndexPath, indexPathForObject inconsistencies
  5. Deleting last row in a section -> crash, using NSFetchedResultsController

问题

  1. 是否有一些指南可以尝试重现和调试 NSInvalidArgumentException
  2. 关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: ios core-data crash


    【解决方案1】:

    在“加载器”类中使用获取结果控制器似乎是错误的方法。

    获取结果控制器实际上是为了帮助您在表格视图中显示核心数据内容。显然,正常的用例是在主线程上运行它。

    如果您从 Web 服务获取数据并可能在成功处理程序中对其进行解析,这意味着它位于不同的线程上。您必须使用单独的上下文(通常是主上下文的子上下文),然后在完成数据操作后保存此上下文。这会将更改推送到主上下文,并且获取的结果控制器将通过委托通知。

    错误消息似乎还表明您没有正确管理上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      相关资源
      最近更新 更多