Ios 无法使用FetchedResultsController生成旧节计数为1、新节计数为0的新节映射

Ios 无法使用FetchedResultsController生成旧节计数为1、新节计数为0的新节映射,ios,swift,uitableview,core-data,swift3,Ios,Swift,Uitableview,Core Data,Swift3,在我的视图控制器中,我有一个实现的fetchedResultsController,一切正常(显示行、多个部分、抓取等) 但是,当我试图更新或删除节中的最后一行时,出现了一个错误(当我说“最后一行”时,我的意思是节中只剩下一行)。如果节中有多行,当我更新它的值(在核心数据中)时,它不会发生错误。我读过,但它对我没有帮助(解决方案已经过时),而且我还没有在StackOverflow和internet上找到任何解决问题的方法。我需要帮助。我使用的是Xcode 8.3.3和Swift 3 以下是我在尝

在我的视图控制器中,我有一个实现的fetchedResultsController,一切正常(显示行、多个部分、抓取等)

但是,当我试图更新或删除节中的最后一行时,出现了一个错误(当我说“最后一行”时,我的意思是节中只剩下一行)。如果节中有多行,当我更新它的值(在核心数据中)时,它不会发生错误。我读过,但它对我没有帮助(解决方案已经过时),而且我还没有在StackOverflow和internet上找到任何解决问题的方法。我需要帮助。我使用的是Xcode 8.3.3和Swift 3

以下是我在尝试更新tableView任何部分中的“Only”行时遇到的错误:

CoreData: error: Serious application error.  
An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 
 UITableView internal bug: unable to generate a new section map with old section count: 
1 and new section count: 0 with userInfo (null)
我使用此方法返回节数:

  func numberOfSections(in tableView: UITableView) -> Int {

           return fetchedResultsController?.sections?.count ?? 0

    }
这是获取行数的方法:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if let sections = fetchedResultsController.sections {

            let currentSection = sections[section]
            return currentSection.numberOfObjects
        }

        return 0

    }
我的视图控制器中还有以下方法:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.beginUpdates()
}

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    self.tableView.endUpdates()
}



func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
        switch type {
        case .delete:
            guard let path = indexPath
                else { return }
            tableView.deleteRows(at: [path],
                                 with: .none)

        case .insert:
            guard let path = newIndexPath
                else { return }
            tableView.insertRows(at: [path],
                                 with: .automatic)

        case .move:

            guard let _ = indexPath,
                let _ = newIndexPath
                else { return }

            if indexPath != newIndexPath {
                tableView.deleteRows(at: [indexPath!], with: .none)
                tableView.insertRows(at: [newIndexPath!], with: .none)
            }


        case .update:

            guard let path = indexPath
                else { return }

            tableView.reloadRows(at: [path], with: .none) // No blinking with .none

        }

    }
}

但我在这件事上又犯了一个错误

  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.
  *** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray with userInfo (null)

经过一些尝试,我找到了一个解决方案:

添加此方法后,我没有任何错误:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
            switch type {
            case .insert:
                tableView.insertSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            case .delete:
                tableView.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            case .move:
                break
            case .update:
                break
            }
        }
func控制器(controller:NSFetchedResultsController,didChange sectionInfo:NSFetchedResultsSectionInfo,atSectionIndex sectionIndex:Int,对于类型:NSFetchedResultsChangeType){
开关类型{
案例.插入:
tableView.insertSections(NSIndexSet(索引:sectionIndex)作为IndexSet,带有:.fade)
案例.删除:
tableView.deleteSections(NSIndexSet(索引:sectionIndex)作为IndexSet,带有:.fade)
案件.动议:
打破
案例。更新:
打破
}
}

由于
beginupdate
endupdate
的原因,有时会发生崩溃。删除并检查@user3589771@user3589771我已尝试删除self.tableView.beginUpdate()和self.tableView.endUpdates()调用,但这并不能解决问题,导致另一个崩溃:“无效更新:无效节数。”。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(2),加上或减去插入或删除的节数(0插入,0删除)。'这几周来一直让我头疼。谢谢你的解决方案!
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
            switch type {
            case .insert:
                tableView.insertSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            case .delete:
                tableView.deleteSections(NSIndexSet(index: sectionIndex) as IndexSet, with: .fade)
            case .move:
                break
            case .update:
                break
            }
        }