iOS 8:从NSFetchedResultsController的委托中捕获到异常,无效更新:第0部分中的行数无效

时间:2016-02-18 09:45:56

标签: ios swift ios8 nsfetchedresultscontroller

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

  

--- --- STARTED
  --------我
  --------我
  --------我
  --- FINISHED ---
  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:

  

--- --- STARTED
  -------- 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上都不起作用。

1 个答案:

答案 0 :(得分:0)

这看起来像是一个过去曾经面临的错误。请尝试下面的代码,也许有帮助。

- (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;
    }
}

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