自定义部分名称崩溃NSFetchedResultsController

时间:2010-03-19 07:11:06

标签: iphone uitableview core-data nsfetchedresultscontroller

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

该表最初显示正常,显示未完成的项目,其dueDate未在标题为“进行中”的部分中传递。现在,用户可以在表视图中点击一行,将新的详细信息视图推送到导航堆栈。在这个新视图中,用户可以点击按钮以指示该项目现在是“完成”。这是按钮的处理程序(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。在任一控制器之前抛出异常: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];
}

2 个答案:

答案 0 :(得分:2)

崩溃是由NSFetchedResultsController导致事先不知道“完成”类别因此崩溃造成的。在其他问题中,我已经看过其他几次这种情况,我建议每次向Apple提交雷达票。这是NSFetchedResultsController中的错误。

答案 1 :(得分:1)

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

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

相关问题