更改 sectionNameKeyPath 属性项时 NSFetchedResultsController 崩溃 属性

NSFetchedResultsController crash when changing sectionNameKeyPath attribute item property

我有一个由 NSFetchedResultsController 支持的 UITableView。

table 视图显示聊天对话。

我有一些聊天对话类型 (NSNumber),table 视图根据您的聊天对话类型分为多个部分,因此,如果您有 3 种对话类型,您的 table 视图。

按日期排序的聊天对话(每个对话包含最后一条消息 dade)。

问题是当用户更新聊天对话类型时,应用程序崩溃并出现以下错误:

CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  *** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0] with userInfo (null)
2015-12-14 18:18:24.831 Reporty[1328:108995] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我使用以下代码初始化 NSFetchedResultsController:

  lazy var fetchedResultsController: NSFetchedResultsController = {

    let fetchRequest = NSFetchRequest(entityName: "ChatConversation")
    fetchRequest.sortDescriptors = [
      NSSortDescriptor(key: "conversationType", ascending: false),
      NSSortDescriptor(key: "lastMessageDate", ascending: false)
    ]

    let frc = NSFetchedResultsController(
      fetchRequest: fetchRequest,
      managedObjectContext: self.chatManager.getChatMainContext(),
      sectionNameKeyPath: "conversationType",
      cacheName: nil)

    frc.delegate = self

    return frc
  }()

我的 NSFetchedResultsController 委托代码:

extension ChatConversationsViewController: NSFetchedResultsControllerDelegate {

  func controllerWillChangeContent(controller: NSFetchedResultsController) {
    tableView.beginUpdates()
  }

  func controller(
    controller: NSFetchedResultsController,
    didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
    atIndex sectionIndex: Int,
    forChangeType type: NSFetchedResultsChangeType) {
      switch type {
      case .Insert:
        tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
      case .Delete:
        tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
      default:
        break
      }
  }

  func controller(
    controller: NSFetchedResultsController,
    didChangeObject anObject: AnyObject,
    atIndexPath indexPath: NSIndexPath?,
    forChangeType type: NSFetchedResultsChangeType,
    newIndexPath: NSIndexPath?) {
      switch type {
      case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
      case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
      case .Update:
        configureCell(tableView.cellForRowAtIndexPath(indexPath!) as! ChatConversationTableViewCell, atIndexPath: indexPath!)
      case .Move:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
      }
  }

  func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.endUpdates()
  }
}

所有核心数据模型都更新为相同的 NSManagedObjectContext。

我正在使用 https://github.com/jessesquires/JSQCoreDataKit 包装器

self.stack!.mainContext.performBlockAndWait {

//update the model
saveContext(self.stack!.mainContext) { (result: CoreDataSaveResult) in
              switch result {
              case .Success():
                print("Success")
              case .Failure(let error):
                print("Error: \(error)")
              }
            }
}

有什么建议吗?

谢谢

NSFetchedResultsControllerDelegate 方法在 NSManagedObjectContextObjectsDidChangeNotification 处理程序中调用。您应该在这些方法的实现中放置断点,以便找到有问题的代码。很有可能它与操纵您的 table 视图和 numberOfSectionsInTableView:tableView:numberOfRowsInSection: 没有返回适当的值有关(因此这些方法中的断点也可能有用)。