简体   繁体   English

NSFetchedResultsController-委托方法在iPhone OS 3.0下崩溃,但不低于3.1

[英]NSFetchedResultsController - Delegate methods crashing under iPhone OS 3.0, but NOT UNDER 3.1

Hey guys, so I've got my NSFetchedResultsController working fine under the 3.1 SDK, however I start getting some weird errors, specifically in the delegate methods when I try it under 3.0. 大家好,所以我的NSFetchedResultsController在3.1 SDK下可以正常工作,但是我开始遇到一些奇怪的错误,尤其是在3.0下尝试使用委托方法时。 I've determined that this is related to the NSFetchedResultsControllerDelegate methods. 我确定这与NSFetchedResultsControllerDelegate方法有关。 This is what I have set up. 这是我设置的。

The inEditingMode stuff has to do with the way I've implemented adding another static section to the table. inEditingMode东西与我实现向表中添加另一个静态部分的方式有关。

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
    NSIndexSet *sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex];

    if(self.inEditingMode){
        sectionSet = [NSIndexSet indexSetWithIndex:sectionIndex + 1];
    }

    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:sectionSet withRowAnimation:UITableViewRowAnimationFade];
            break;
        default:
            [self.tableView reloadData];
            break;

    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
    NSIndexPath *relativeIndexPath = indexPath;
    NSIndexPath *relativeNewIndexPath = newIndexPath;

    if(self.inEditingMode){
        relativeIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section + 1];
        relativeNewIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row inSection:newIndexPath.section + 1];
    }

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:relativeNewIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:relativeIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        default:
            [self.tableView reloadData];
            break;
    }
}


-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
    [self.tableView endUpdates];
}

When I add an entity to the managed object context, I get the following error: 将实体添加到托管对象上下文时,出现以下错误:

Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)

I put a breakpoint on objc_exception_throw, and the crash seems to be occuring inside of controllerDidChangeContent. 我在objc_exception_throw上设置了一个断点,崩溃似乎发生在controllerDidChangeContent内部。

If I comment out all of the self.tableView methods, and put a single [self.tableView reloadData] inside of controllerDidChangeContent, everything works as expected. 如果我注释掉所有self.tableView方法,并在controllerDidChangeContent内放置一个[self.tableView reloadData],则一切都会按预期进行。

Anybody have any idea as to why this is happening? 任何人都知道为什么会这样吗?

In the documentation for NSFetchedResultsController, there is specific mention of a bug in the 3.0 implementation that results in a discrepancy between the number of sections reported by the controller and the number of sections expected by the UITableView. 在NSFetchedResultsController的文档中,特别提到3.0实现中的一个错误,该错误导致控制器报告的节数与UITableView期望的节数之间存在差异。 This is the workaround they provide: 这是他们提供的解决方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    NSUInteger count = [[<#Fetched results controller#> sections] count];
    if (count == 0) {
        count = 1;
    }
    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *sections = [<#Fetched results controller#> sections];
    NSUInteger count = 0;
    if ([sections count]) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
        count = [sectionInfo numberOfObjects];
    }
    return count;
}

Note that this workaround is not required for OS 3.1, so this may explain why you are not seeing errors. 请注意,OS 3.1不需要此解决方法,因此这可以解释为什么您没有看到错误。 The workaround is only necessary in 3.0 when sectionNameKeyPath is set to nil. 仅当sectionNameKeyPath设置为nil时,才需要在3.0中解决该问题。 If you are setting a value for sectionNameKeyPath, then this is likely not the issue. 如果要为sectionNameKeyPath设置值,则可能不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM