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

标签 iphone uitableview core-data nsfetchedresultscontroller

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

- (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"];

表格最初显示良好,在标题为“进行中”的部分中显示了到期日期尚未过去的未完成项目。现在,用户可以点击 TableView 中的一行,将新的详细信息 View 推送到导航堆栈上。在这个新 View 中,用户可以点击按钮来指示该项目现在已“完成”。这是按钮的处理程序(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'

我设法找出当前被新详细信息 View 隐藏的 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 属性更改后读回时类别会有所不同。

请帮忙!

更新:

这是我的 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 中的一个错误。

关于iphone - 自定义部分名称导致 NSFetchedResultsController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2475542/

相关文章:

ios - 如何编辑核心数据值然后保存上下文以覆盖旧值?

cocoa - 将对象添加到 NSArrayController 并将其保存到实体中

ios - 创建 p2p 连接 iOS

iPhone 如何设置 View 倒置模式

iphone - 如何在没有分隔符的情况下分割字符串?

ios - 在 UITableViewCell 中处理按钮单击

ios - NSPredicate 用于至少有一个具有属性值的相关实体的实体

ios - 展开后 swift subview 不更新

iphone - 将数据从我的详细 View 发送回我的 UITableView

ios - 在 iOS 中将单元格从一个表格移动到另一个表格