gpt4 book ai didi

ios - NSFetchedResultsController XCode 7 问题

转载 作者:IT王子 更新时间:2023-10-29 05:42:29 26 4
gpt4 key购买 nike

Xcode 7 beta 6 和 NSFetchedResultsController 今天让我头疼。如果我用 Xcode 6 编译相同的代码(使用 Swift 2 修复),程序可以在设备和模拟器(iOS 7、iOS8)上运行。但是,如果我使用 Xcode 7 beta 6 进行编译,则每当我更新 Core Data 时,程序都会在设备 (iOS 8.4) 上崩溃并显示以下错误消息。

我收到类似下面的错误

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 (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (2 inserted, 1 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)

有时它会因另一个错误而崩溃

Invalid pointer dequeued from free list *** set a breakpoint in malloc_error_break to debug

//NSFetchedResultsController delegates



func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case .Insert:
self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
case .Delete:
self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
default:
return
}
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
print("Insert New: \(newIndexPath) Old: \(indexPath)")
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
print("Delete New: \(newIndexPath) Old: \(indexPath)")
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
print("Update New: \(newIndexPath) Old: \(indexPath)")
tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
//self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
case .Move:
print("Move New: \(newIndexPath) Old: \(indexPath)")
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
self.tableView.endUpdates()
}

对于 .Update 部分我也尝试调用

 self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)

配置单元格方法:

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) { 
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Person
//etc
}

排序描述符

let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]

程序总是崩溃。以某种方式阻止此问题的唯一方法是将 delagete 设置为 nil,更新对象并将 delagate 设置回 self。

这是一个错误吗?如果不是,我该如何防止这种情况发生?谢谢

一些具有相同/相似问题的错误报告

https://forums.developer.apple.com/thread/12184

https://forums.developer.apple.com/thread/4999

iOS 9 - "attempt to delete and reload the same index path"

我同时运行了 XCode6 和 XCode7 版本并打印了 didChangeObject 方法这里是比较

XCode6 iOS 8.4

Update New: Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) Old: Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})

XCode 7b6 iOS 8.4

Update New: nil Old: Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})
Insert New: Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) Old: Optional(<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})

2015-09-01 01:15:37.898 TestProg[3230:200562] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-3347.44.2/UITableView.m:1623
2015-09-01 01:15:37.900 TestProg[3230:200562] 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 (3) must be equal to the number of rows contained in that section before the update (3), 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)

最佳答案

我已经尝试了几种方法,我相信这是一个错误。正如您从我的日志中看到的那样,即使我更新了现有记录,委托(delegate)人也会收到插入消息。在 .Insert 添加条件检查解决了我的问题。我没有在表中使用任何移动操作。因此,如果您使用 .Move 我当前的代码如下

,则可能需要相同类型的条件操作
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
//FIXME: iOS 9 Bug!
if indexPath != newIndexPath {
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
// self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)

case .Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
}
}

我已经尝试更新单条记录、多条记录,但到目前为止我还没有看到任何崩溃。我正在使用

tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)

方法但是

self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)

在我的测试中也有效。

关于ios - NSFetchedResultsController XCode 7 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32318976/

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