NSFetchedResultsController XCode 7 问题

2024-04-26

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

我收到类似于下面的错误

CoreData:错误:严重的应用程序错误。捕获了异常 在调用期间从 NSFetchedResultsController 的委托 -controllerDidChangeContent:。无效更新:第 0 节中的行数无效。现有节中包含的行数 更新后 (2) 必须等于包含的行数 更新之前的部分 (2),加上或减去行数 从该部分插入或删除(2 个插入,1 个删除)以及加上 或减去移入或移出该部分的行数(0 移动 入,0 移出)。与用户信息(空)

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

无效指针从空闲列表中出列 *** 在malloc_error_break中设置断点进行调试

//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()
}

for the .Update我也尝试打电话

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

配置cell方法:

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/12184

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

iOS 9 - “尝试删除并重新加载相同的索引路径” https://stackoverflow.com/questions/31383760/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)

我已经尝试了几件事,我相信这是一个错误。正如您从我的日志中看到的,即使我更新现有记录,委托也会收到插入消息。添加条件检查.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!)

在我的测试期间也有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSFetchedResultsController XCode 7 问题 的相关文章

  • 自定义 UITableViewRowAnimation 或持续时间

    我有一个用户可以拖动的应用程序UITableViewCells从一TableView到另一个 我通过在用户触摸的 UITableViewCell 顶部渲染一个 虚拟 单元格来实现此目的 并禁用 真实 单元格 然后我在目标中插入一个新行UIT
  • CocoaPods 库中的强dispatch_queue_t

    在可能使用 iOS 5 x OS X 10 7 部署目标或较新的部署目标构建的库中 我在正确定义dispatch queue t财产 大多数情况下我可以按照建议解决它here https stackoverflow com a 248460
  • NSTimer 每分钟运行一次,但在第一秒运行

    我有使用 NSTimer 每分钟运行一次的经验 例如 NSTimer scheduledTimerWithTimeInterval 60 0 target self selector Selector everyMinute userInf
  • 如何按日期正确对从 CoreData 获取的列表进行分组?

    为了简单起见 假设我想创建一个简单的待办事项应用程序 我的 xcdatamodeld 中有一个实体 Todo 其属性id title and date 以及以下 swiftui 视图 如图所示的示例 import SwiftUI struc
  • 如何从 os_log() 查找源文件和行号

    The 记录 Apple 参考 https developer apple com reference os 1891852 logging对于 iOS 10 和 macOS Sierra 中的新日志记录系统 明确表示不要包含行号和源文件信
  • clickedButtonAtIndex 方法未被调用

    当用户点击按钮时UIAlertView the clickedButtonAtIndex应该调用方法 但是 它没有 in the h我已经打电话给UIAlertView协议 interface RechercherViewControlle
  • 如何简化 Swift Enum 自定义 init

    我创建了一个带有字符串类型的枚举 它有两个 init 方法 一种是使用 rawValue 的默认 init 方法 另一种是使用 intValue 的自定义 init 方法 我是这样写的 有没有什么简单的方法可以不使用两个开关盒 enum R
  • 在 Xcode 中添加较大的 Power 值

    我是 Objective C 和 iPhone 开发的新手 我正在使用基本计算器 我想在文本字段中添加一个大值 如何在 Xcode 中显示像这样的大值 6 67543 x 10 34 谢谢 您可以使用 NSNumberFormatterSc
  • 在真实设备上通过命令行实现 UIAutomation

    我知道从Xcode 4 2可以运行UIAutomation通过命令行编写脚本 我已经尝试过这个并且在模拟器中对我来说工作得非常好 我想知道如何在实际设备中运行此命令 我搜索并获取了在设备上运行的命令 instruments w
  • sqlite 3“SQL 错误‘内存不足’(7)”objc

    嗨 有人可以指出我做错了什么吗 错误是这样的 SQL error out of memory 7 NSArray RecipeInfo NSMutableArray retval NSMutableArray alloc init NSSt
  • 使用多个 DispatchQueue.main.async 查看冻结

    视图冻结而数据是获取并显示 以我的理解fetchBoard and initUserInfo 不要并行执行 因为视图仅在以下情况下加载fetchBoard 加载板 我担心如果使用DispatchQueue main async多次冻结视图
  • UITableViewCell 中的自定义 VoiceOver 操作

    When a UITableView是可编辑的 其UITableViewCells允许用户在 VoiceOver 打开时执行自定义操作 当 VoiceOver 光标位于单元格上时 用户可以通过向上或向下滑动来听到可用的操作 然后通过双击屏幕
  • Swift 3 中的隐藏按钮

    我刚刚开始编码 我真的很想知道如何隐藏按钮 我想做的是当我按下按钮时 Start 我想让开始按钮和后退按钮消失 这是通过插座和操作完成的 我已经搜索了一下 但是当我想做的时候 它说do关键字应该指定一个语句块 我希望有人能帮助我 连接插座后
  • 长按 UIButton

    我想知道如果有人按住 UIButton 按键的时间过长 我是否可以捕获 UIButton 的事件 通过通知或其他机制 比按一次按钮的时间更长 假设有人按住按钮几秒钟 谢谢 你可以加UILongPressGestureRecognizer h
  • SwiftUI:如何横向呈现全屏覆盖

    我正在制作一个应用程序 其中多个页面 视图嵌入到具有选项卡栏 菜单栏和标题的父视图中 如下所示 HeaderView MenuView TabView selection selected HomeView Page2View Page3V
  • iOS 15 中 UITableView 部分之间的额外空间

    UITableView显示没有节页脚的多个节 节之间有额外的空间 Xcode 的视图调试器显示它不是视图 而只是一个空白区域 就我而言 这种行为是不受欢迎的 添加 1 0 0 0 高度页脚并没有帮助 更改表视图也不行style 这是示例代码
  • 尝试在iPhone上生成随机数,得到相同的数字[重复]

    这个问题在这里已经有答案了 我正在尝试制作一款纸牌游戏 我正在使用以下代码来绘制随机卡 iCard 随机 55 但 icard 总是以 28 开头 似乎它以相同的顺序返回数字 有没有办法在每次第一次调用该函数时获得不同的随机数 random
  • 可选函数与抛出函数

    考虑我编写的以下查找函数 该函数使用可选值和可选绑定 如果在字典中找不到键则报告一条消息 func lookUp
  • 错误:将构建上传到 iTunes Connect 时,Swift 支持无效

    我正在提交 TestFlight 发行版的第一个版本 但收到以下错误 位码已关闭 其他答案似乎相当旧 所以我想我会在 2018 年重新询问 无效的 Swift 支持 文件 libswiftDarwin dylib libswiftMetal
  • Xcode -- 让force_load 使用相对路径

    某些库在链接到 Xcode 项目时需要 all load 链接器标志 但是 如果库之间存在符号冲突 这会导致链接器错误 解决方案是使用 force load 它可以有效地让您在某些库上使用 all load 但不能在其他库上使用 然而 这反

随机推荐