【问题标题】:Custom Section Name Crashing NSFetchedResultsController自定义部分名称崩溃 NSFetchedResultsController
【发布时间】:2011-01-29 08:27:52
【问题描述】:

我有一个带有 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 属性永远不会改变,而在finishedDate 属性更改后回读时类别会有所不同。

请帮忙!

更新:

这是我的 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];
}

【问题讨论】:

    标签: iphone uitableview core-data nsfetchedresultscontroller


    【解决方案1】:

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

    【讨论】:

    • 刚刚偶然发现了这个问题,因为我的应用程序在 iOS 3 下崩溃正是出于这个原因,但在 iOS 4 下却没有。必须找到解决方法或放弃 3.x 支持...
    【解决方案2】:

    在我看来,您的问题在于您用于提供sectionNameKeyPath 的“类别”瞬态属性。 sectionNameKeyPath 的顺序必须与主排序描述符相同。在您的情况下,这意味着所有“过期”任务的日期必须早于所有“完成”任务的日期必须早于所有“进行中”任务的日期。可以构建一个场景,其中“完成”任务有一个dueDate,它出现在“进行中”任务之后或“过期”任务之前。这种情况破坏了sectionNameKeyPath 的排序要求,并导致NSFetchedResultsController 抛出NSInternalConsistencyException

    我为您的问题提出了一个解决方案,该解决方案不涉及滚动您自己的数组,然后必须将其拆分为多个部分。在模型中创建一个整数属性,将 0 映射到“过期”,将 1 映射到“完成”,将 2 映射到“进行中”。将此作为NSFetchRequest 中的主要排序描述符并按升序对该属性进行排序。将辅助排序描述符添加到NSFetchRequest,以升序对dueDate 属性进行排序。修改您的类别方法以从您在上面创建的整数属性派生类别名称并将其用作您的sectionNameKeyPath。您将需要更新整数属性以更新任务,因为它们从进行中移动到过期完成等。

    【讨论】:

    • 我非常不同意 Marcus 的观点 - 我认为这根本不是错误。 NSFetchedResultsController 的文档指出:“获取请求必须至少有一个排序描述符。如果控制器生成部分,则数组中的第一个排序描述符用于将对象分组为部分;它的键必须与 sectionNameKeyPath 相同或使用其键的相对排序必须与使用 sectionNameKeyPath 的相对排序相匹配。”正如我所看到的,崩溃正在发生,因为排序描述符的键和 sectionNameKeyPath 的键具有不同的排序顺序。
    • 非常有趣的建议。根据我从 SDK 文档中读到的内容,这是有道理的。我已经走上了拥有一个单独数组的路线,但我会看看我是否可以在某个时候尝试一下。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多