我有一个由NSFetchedResultsController支持的UITableView。
表格视图显示聊天对话。
我有一些聊天对话类型(NSNumber),根据您的聊天对话类型分隔各个部分的表格视图,因此,如果您有3个会话类型,则表格视图中有3个部分。
按日期排序的聊天对话(每个对话包含最后一条消息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)")
}
}
}
有什么建议吗?
由于
答案 0 :(得分:1)
在NSFetchedResultsControllerDelegate
处理程序内调用NSManagedObjectContextObjectsDidChangeNotification
方法。您应该在方法实现中添加断点,以便找到有问题的代码。很有可能它与操纵表视图和numberOfSectionsInTableView:
或tableView:numberOfRowsInSection:
没有返回适当的值有关(因此这些方法中的断点也可能有用)。