自定义部分名称导致 NSFetchedResultsController 崩溃

2024-04-05

我有一个带有 dueDate 属性的托管对象。我没有使用一些丑陋的日期字符串作为 UITableView 的节标题进行显示,而是创建了一个名为“category”的瞬态属性,并将其定义如下:

- (NSString*)category
{
    [self willAccessValueForKey:@"category"];

    NSString* categoryName;
    if ([self isOverdue])
    {
        categoryName = @"Overdue";
    }
    else if ([self.finishedDate != nil])
    {
        categoryName = @"Done";
    }
    else
    {
        categoryName = @"In Progress";
    }

    [self didAccessValueForKey:@"category"];
    return categoryName;
}

这是 NSFetchedResultsController 设置:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
                                          inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSMutableArray* descriptors = [[NSMutableArray alloc] init];
NSSortDescriptor *dueDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dueDate"
                                                                  ascending:YES];
[descriptors addObject:dueDateDescriptor];
[dueDateDescriptor release];
[fetchRequest setSortDescriptors:descriptors];

fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"category" cacheName:@"Root"];

该表最初显示良好,在标题为“进行中”的部分中显示了截止日期尚未过去的未完成项目。现在,用户可以点击表视图中的一行,将新的详细信息视图推送到导航堆栈上。在这个新视图中,用户可以点击按钮来指示该项目现在已“完成”。这是按钮的处理程序(self.task 是托管对象):

- (void)taskDoneButtonTapped
{
    self.task.finishedDate = [NSDate date];
}

一旦“finishedDate”属性的值发生变化,我就会遇到以下异常:

2010-03-18 23:29:52.476 MyApp[1637:207] Serious application error.  Exception was caught during Core Data change processing: no section named 'Done' found with userInfo (null)
2010-03-18 23:29:52.477 MyApp[1637:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no section named 'Done' found'

我设法找出当前被新详细信息视图隐藏的 UITableView 正在尝试更新其行和部分,因为 NSFetchedResultsController 被通知数据集中发生了某些变化。这是我的表更新代码(从 Core Data Recipes 示例或 CoreBooks 示例复制 - 我不记得是哪个):

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

我在每个函数中都放置了断点,发现只有controllerWillChange被调用。在调用controller:didChangeObject:atIndexPath:forChangeType:newIndex 或controller:didChangeSection:atIndex:forChangeType 之前引发异常。

此时我被困住了。如果我将我的sectionNameKeyPath更改为“dueDate”,那么一切正常。我认为这是因为 dueDate 属性永远不会改变,而在 finishDate 属性更改后读回时类别会有所不同。

请帮忙!

UPDATE:

这是我的 UITableViewDataSource 代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [self configureCell:cell atIndexPath:indexPath];    

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];    
    return [sectionInfo name];
}

崩溃是由于 NSFetchedResultsController 事先不知道“完成”类别而导致崩溃。我在其他问题中多次看到过这种崩溃,对于每个问题,我都建议向苹果提交雷达票。这是一个错误NSFetchedResultsController.

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

自定义部分名称导致 NSFetchedResultsController 崩溃 的相关文章

随机推荐

  • Groovy:如何在字符串中包含反斜杠而不转义?

    我想在我的 groovy 程序中使用以下字符串文字 而不必转义反斜杠 C dev username 这是我到目前为止所尝试过的 字符串 单引号 和 GString 双引号 def aString C dev username def aGS
  • 在 MATLAB 中加载多个图像

    这是所需的工作流程 我想将 100 张图像加载到 MATLAB 工作区 在图像上运行一堆我的代码 将我的输出 我的代码返回的输出是一个整数数组 保存在一个新数组中 最后 我应该有一个数据结构来存储图像 1 100 的代码输出 我该怎么做呢
  • Javascript:如何在处理程序中保留对请求发起者的引用?

    我通常不是一个 Javascript 人 但我一直在潜心阅读道格拉斯 克罗克福德的书 http oreilly com catalog 9780596517748 并编写一些琐碎 有用的花絮作为 Chrome 扩展和Node js http
  • boost::asio::io_service 在 win_mutex 锁中崩溃

    我一直遇到 boost asio 问题 其中使用全局 io service 实例创建的计时器和 或套接字在构造过程中崩溃 发生崩溃的系统如下 Windows 7的 适用于 Windows 桌面的 Visual Studio 2013 Exp
  • python读取文件utf-8解码问题

    我在读取包含 UTF8 和 ASCII 字符的文件时遇到问题 问题是我使用seek仅读取数据的某些部分 但我不知道我是否在UTF8的 中间 读取 osx 蟒蛇3 6 6 简而言之 我的问题可以用以下代码来演示 write some utf
  • 如何使用 Jquery 创建 .txt 文件?

    好的 这是我的域 example com index html 我想在该域中创建一个 txt 文件 结果 example com file txt 包含以下信息 js saveButton on click function e e pre
  • Python-Sphinx:从超类“继承”方法文档

    Edit 截至目前 Sphinx 1 4 9 似乎没有办法告诉 Sphinx 做我想做的事情 参见issue https github com sphinx doc sphinx issues 3140在 GitHub 上 这接受的答案 h
  • .NET GUI 中仍然使用本机 Windows 控件吗?

    当您使用 WinForms 或 WPF 创建 GUI 时显示的内容仍然基于本机控件 例如通用控制 http msdn microsoft com en us library windows desktop bb773169 28v vs 8
  • Android 接收器可以进行多种操作?

    简单的问题 我可以注册一个吗BroadcastReceiver多个 Intent 操作 这是我正在考虑的
  • linkedin 上的 Open Graph 图片问题

    我的项目现在遇到有关 linkedin 共享的问题 图像 缩略图 未显示 我的问题是 网址更改也会影响吗 例如 我添加了这样的 og 标签 https domain name com wp content uploads 2016 10 2
  • 如何更改 Angular 中 mat-dialog 的 Z 索引

    我的应用程序使用多个 mat dialogs 有时可能会同时显示 2 个 这会导致问题 因为第二个永远不会正确显示 而且它的模式使应用程序变得无用 经过更多研究后 我似乎可以通过调整 mat dialog 的 z index cdk ove
  • 如何获取通过SpriteKit编辑器创建的项目的SKSpriteNode?

    我使用 SpriteKit 使用 Objective C 在 XCode 中创建了一个相当简单的 此时是实验性的 游戏 我知道如何手动创建 SKSpriteNode 对象并将其添加到 SKScene 但我有点尝试做相反的事情 我在 XCod
  • Spark.ml 回归计算的模型与 scikit-learn 不同

    我在 scikit learn 和 Spark ml 中设置一个非常简单的逻辑回归问题 结果有所不同 他们学习的模型不同 但我不明白为什么 数据相同 模型类型是相同 正则化相同 毫无疑问 我错过了一侧或另一侧的一些设置 哪个设置 我应该如何
  • Visual Studio C# - 未找到 SQLite.Interop.dll

    我目前正在尝试使用 Visual Studio 创建一个与 SQLite 一起使用的 C 应用程序 我使用 NuGet 为我的程序安装了 SQLite 解决方案资源管理器中出现了三个引用 System Data SQLite System
  • 刷新 Ajax 成功的数据表

    我正在使用数据表和 jquery 对话框 总的来说 我有 3 个表格和 3 个数据表 我的脚本运行良好 但我遇到的问题是在 ajax 保存成功时更新正确的数据表 它甚至不必是正确的对应表 它可以更新 3 个表单保存中任何一个的所有 3 个表
  • 仅当值既不为 null 也不未定义时才调用函数

    单击按钮时 我检查本地存储键中是否存在某些内容 如下所示 var a localStorage getItem foo if typeof a undefined Function 但如果该键根本不存在 则返回 null 我怎样才能打电话如
  • 为什么“Dispose”有效,而不是“using(var db = new DataContext())”?

    我正在创建一个由主题组成的论坛 主题由消息组成 当我尝试在我的控制器中实现主题视图时 public ActionResult Topic int id Topic Id using var db new DataContext var to
  • Flutter可拖动容器:从上到下扩展

    I want to achieve the following example 如您所见 用户必须能够从上到下拖动 开始时 只能看到图像 但是一旦用户从上到下拖动元素 它将显示更多内容 在扩展橙色容器时 它应该高于所有其他绿色元素 我调查过
  • 如何使用 C#、.NET 将文本写入 Word 文件

    我正在尝试使用 C 编写一些文本并将其附加到 Word 文件中 但是 我无法获得预期结果 你能帮我解决这个问题吗 下面是我的代码 using System using System Collections Generic using Sys
  • 自定义部分名称导致 NSFetchedResultsController 崩溃

    我有一个带有 dueDate 属性的托管对象 我没有使用一些丑陋的日期字符串作为 UITableView 的节标题进行显示 而是创建了一个名为 category 的瞬态属性 并将其定义如下 NSString category self wi