gpt4 book ai didi

ios - CoreData 在插入和删除同时发生时崩溃

转载 作者:行者123 更新时间:2023-11-29 05:39:49 25 4
gpt4 key购买 nike

我在 FetchedResultsController 上遇到 CoreData 崩溃。

场景:我尝试在 FetchedResultsController 上同时插入和删除一些数据。所以,在这段时间里,崩溃发生在意料之外。

注意:崩溃很少发生。

我尝试了 StackOverflow 的一些答案,但没有一个对我有用。因此,找到解决此问题的方法将很有帮助。

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {

switch type {

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

case .delete:
guard let indexPath = indexPath else { return }
self.tableView.deleteRows(at: [indexPath], with: .none)

case .update:
guard let indexPath = indexPath else { return }
self.updateRow(atIndexpath: indexPath)

case .move:

guard let deleteIndexPath = indexPath, let insertIndexPath = newIndexPath, deleteIndexPath == insertIndexPath else {
return
}
self.updateRow(atIndexpath: insertIndexPath)
}
}

崩溃报告:

*** Assertion failure in -[AppName.tableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.119.2/UITableView.m:1821

[error] fault: 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 7. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), 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)

我对这个问题有一些线索,它发生在 controllerDidChangeContent 委托(delegate)的 tableView.endUpdates() 中。

最佳答案

您没有移动 .move 案例中的任何内容。有一个API:

case .move: tableView.moveRow(at: indexPath!, to: newIndexPath!)

强制展开路径是安全的,因为 Controller 会发送 .move 情况下的两条路径以及其他情况下的相应路径。

关于ios - CoreData 在插入和删除同时发生时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56670702/

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