2

我有一个带有 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];
}
4

2 回答 2

2

The crash is being caused by the NSFetchedResultsController not knowing about the "done" category before hand and therefore crashing. I have seen this crash a few other times in other questions and with each one I recommend submitting a radar ticket to Apple. This is a bug in the NSFetchedResultsController.

于 2010-03-27T14:32:36.893 回答
1

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

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

于 2010-03-27T08:22:09.693 回答