【发布时间】:2016-02-18 09:45:55
【问题描述】:
来自我的NSFetchedResultsControllerDelegate 的完整日志,适用于 iOS 8:
---开始---
--------我
--------我
--------我
---完成---
2016-02-18 10:42:22.433 POSowner[9784:66814] *** 断言失败 -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623 2016-02-18 10:42:22.440 POSowner[9784:66814] CoreData:错误:严重的应用程序错误。在调用 -controllerDidChangeContent: 期间,从 NSFetchedResultsController 的委托中捕获了一个异常。无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (1) 必须等于更新前该节中包含的行数 (1),加上或减去数字从该部分插入或删除的行数(1 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。使用 userInfo (null)
---开始---
--------D
--------D
--------D
---完成---
对于 iOS 9:
---开始---
--------D
--------D
--------D
--------我
--------我
--------我
---完成---
这是我的NSFetchedResultsControllerDelegate:
//MARK: - NSFetchedResultsControllerDelegate
func controllerWillChangeContent(controller: NSFetchedResultsController) {
print("---STARTED---")
tableView.beginUpdates()
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
let indexSet = NSIndexSet(index: sectionIndex)
switch type {
case .Insert:
tableView.insertSections(indexSet, withRowAnimation: .Fade)
case .Delete:
tableView.deleteSections(indexSet, withRowAnimation: .Fade)
case .Update:
fallthrough
case .Move:
tableView.reloadSections(indexSet, withRowAnimation: .Fade)
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
print("--------I")
if let newIndexPath = newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
case .Delete:
print("--------D")
if let indexPath = indexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
case .Update:
print("--------U")
if let indexPath = indexPath, let cell = tableView.cellForRowAtIndexPath(indexPath) as? PBOTableViewCell {
updateCell(cell, indexPath: indexPath)
}
case .Move:
print("--------M")
if let indexPath = indexPath {
if let newIndexPath = newIndexPath {
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade)
}
}
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
print("---FINISHED---")
tableView.endUpdates()
}
和UITableViewDataSourceDelegate:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
var numberOfSections = 0
if let sections = fetchedResultsController?.sections {
numberOfSections = sections.count
}
return numberOfSections
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let workday = workDayObjectForSection(section)
return workday.worktimes.count
}
private func workDayObjectForSection(section: Int) -> PBOUserWorkDay {
let sections: NSArray? = fetchedResultsController.sections!
let sectionInfo = sections?.objectAtIndex(section) as? NSFetchedResultsSectionInfo
let workdays = sectionInfo?.objects as! [PBOUserWorkDay]
return workdays[0]
}
它仅在 iOS 8 上发生。有任何解决方法吗? 完全相同的操作在 iOS 9 上就像一个魅力。而且每次它都不适用于 iOS 8。
【问题讨论】:
-
您确定您正在编辑的项目数量正确吗?我多次遇到同样的问题,这是我插入新数据时的逻辑错误
-
@bobby,我更新了问题。如果项目数不正确,则在 iOS 9 上将无法运行。如果您需要,我可以添加更多代码...
-
所以你是说在iOS9上使用相同的数据(完全相同的数据)你不会崩溃,对吧?
-
我假设您正在使用 Core Data,并且 iOS9 的 Core Data 具有诸如 unicity 之类的新功能。所以它可能在 iOS 9 上工作,因为 FatchController 检测到你只是在编辑一个条目,而 iOS 8 失败因为它认为你正在添加一个新条目,但你没有
-
是的,数据一模一样,在ios 9上没有崩溃。经过艰苦的测试。当然,我使用核心数据。你可能是对的......任何想法如何解决这个问题?我可以试试……
标签: ios swift ios8 nsfetchedresultscontroller