【问题标题】:Sort Sections in NSFetchedResultsController在 NSFetchedResultsController 中对部分进行排序
【发布时间】:2021-03-05 14:59:44
【问题描述】:

我正在使用 NSFetchedResultsController 来管理我的 tableView,问题是当添加另一个部分时,NSFetchedResultsController 自动将它放在索引 0 处,这导致我的应用程序出现了几个问题。下面是我如何设置我的 NSFetchedResultsController,我正在获取类型 CDSet,每个部分的类型都是 exercise

两者的关系如下 CDSet Exercise

如何按exercise.exerciseIndex 对部分进行排序?

let fetchRequest:NSFetchRequest<CDSet> = CDSet.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "set_index", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "exercise.workout == %@", currentWorkout)

fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: DataController.shared.context, sectionNameKeyPath: "exercise.exerciseIndex", cacheName: nil)


fetchedResultsController.delegate = self

do {
    try fetchedResultsController.performFetch()
} catch {
    
}

更新: 我已将 sortDescriptor 更新为以下内容:

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "exercise.exerciseIndex", ascending: true), NSSortDescriptor(key: "set_index", ascending: true)]

但现在尝试添加第二个部分时出现此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (2 inserted, 0 deleted).'

当我只指定 1 时,不确定它为什么要插入 2

这是我的 NSFetchedResultsController 委托方法:

func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        tableView.beginUpdates()
    }
    
    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        tableView.endUpdates()
    }
    
    
    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
        
        let indexPath = IndexPath(row: 0, section: sectionIndex)
        let indexSet = IndexSet(indexPath)
        tableView.insertSections(indexSet, with: .fade)
    }
    
    func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
        
        
        switch type {
        case .insert:
            tableView.insertRows(at: [newIndexPath!], with: .fade)
        case .delete:
            tableView.deleteRows(at: [newIndexPath!], with: .fade)
        default:
            break
        }
    }

【问题讨论】:

  • 那么为什么不根据excercise.index(或任何要排序的值)和set_index 进行排序呢?

标签: ios swift core-data nsfetchedresultscontroller


【解决方案1】:

您必须指定两个排序描述符。

  • 第一个对section进行排序,必须与section key path等价。
  • 第二个对部分中的项目进行排序。

编辑

controller(_:didChange: atSectionIndex:for:)的通常实现是

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
      switch type {
          case .insert: self.tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
          case .delete: self.tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
          default: return
      }
  }

【讨论】:

  • 我试过了,但无法判断它是否真的有效,在添加第二部分后,我的应用程序崩溃并给我这个错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (2 inserted, 0 deleted)
  • 你是否正确实现了委托方法,尤其是didChangeSection
  • 我相信我做到了,我用我所有的委托功能更新了帖子
猜你喜欢
  • 2013-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
相关资源
最近更新 更多