gpt4 book ai didi

iOS 8 : An exception was caught from the delegate of NSFetchedResultsController, 更新无效:第 0 节中的行数无效

转载 作者:行者123 更新时间:2023-11-28 08:45:19 24 4
gpt4 key购买 nike

从我的 NSFetchedResultsControllerDelegate for iOS 8 完成日志:

---STARTED---
--------I
--------I
--------I
---FINISHED---
2016-02-18 10:42:22.433 POSowner[9784:66814] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623 2016-02-18 10:42:22.440 POSowner[9784:66814] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
---STARTED---
--------D
--------D
--------D
---FINISHED---

对于 iOS 9:

---STARTED---
--------D
--------D
--------D
--------I
--------I
--------I
---FINISHED---

这是我的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 上的效果就像一个魅力。而且 EVERY TIME 它在 iOS 8 上不起作用。

最佳答案

这看起来像是一个错误,我过去遇到过。请尝试下面的代码,也许它有帮助。

- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{

switch (type) {

case NSFetchedResultsChangeDelete: {
// your code
} break;

case NSFetchedResultsChangeInsert: {
// your code
} break;

case NSFetchedResultsChangeUpdate: {
// your code
} break;

case NSFetchedResultsChangeMove: {
// BUG: Move is called where Update is expected
if ([indexPath compare:newIndexPath] == NSOrderedSame) {
// object is not moved, so perfrom the same logic as for NSFetchedResultsChangeUpdate

// your code
}
else {
// object is moved, actually

// your code
}
} break;

default:
break;
}
}

我不记得在哪里找到了正确方向的解决方案/想法。

关于iOS 8 : An exception was caught from the delegate of NSFetchedResultsController, 更新无效:第 0 节中的行数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35477892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com