tableView endUpdates在fetchedResults中更改后崩溃

时间:2010-12-18 23:46:05

标签: iphone uitableview core-data ios

我有一个应用程序,当用户首次启动应用程序时,它会显示一个自定义的无数据单元格。

当用户进行第一次输入时,我的fetchedResultsController的fetchedResults会更新,这会导致删除无数据单元格并插入数据单元格。

这曾经在过去工作过(iPhone 3.x)。现在在iOS 4.2上,它会在调用endUpdates后导致崩溃。没有异常信息或任何可理解的堆栈跟踪。我只知道崩溃是在_CFTypeCollectionRetain中引起的(可能试图保留NULL)

任何想法如何进行?

这是相关代码:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"starting updates");
    [self.tableView beginUpdates];
}


- (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)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tv = self.tableView;

    [tv reloadData];
    switch(type) {

        case NSFetchedResultsChangeInsert:
            NSLog(@"insert");

            if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 1 &&
                [[self.fetchedResultsController fetchedObjects] count] == 1) 
            {
                NSLog(@"reloading row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            else {
                NSLog(@"inserting new row %d", newIndexPath.row);
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeDelete:
            NSLog(@"delete");


            if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 0 &&
                [[self.fetchedResultsController fetchedObjects] count] == 0) 
            {
                NSLog(@"reloading row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

            }
            else {
                NSLog(@"deleting row %d", newIndexPath.row);
                [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            }

            break;

    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"finishing updates");
    [self.tableView endUpdates];
}

1 个答案:

答案 0 :(得分:5)

在委托方法

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath 

您处理插入和删除。对于插入indexPathnil。对于删除newIndexPathnil。因此,如果newIndexPath,则不允许您访问type== NSFetchedResultsChangeDelete

此外,此处不需要拨打reloadData