具有 NSManaged 对象上下文的 TableView 部分抛出异常

TableView sections with NSManaged Object Context Throws Exception

我最近使用配置单例从自我管理对象迁移了一个项目,以将 NSManaged 对象上下文与 NSFetchedResultController 结合使用。我想要做的是用基于月份的部分填充 TableView,但是用户可以 select 一个单元格并更改月份。当发生这种情况时,它会导致抛出以下异常并且单元格变得无法更改或编辑

    [error] error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to insert row 0 into section 1, but there are only 0 sections after the update with userInfo (null)
CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to insert row 0 into section 1, but there are only 0 sections after the update with userInfo (null)

这是主视图控制器获取请求:

- (NSFetchedResultsController<Budget *> *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    LogDebug(@"STARTED");
    NSFetchRequest<Budget *> *fetchRequest = Budget.fetchRequest;
    [fetchRequest setFetchBatchSize:20];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    NSFetchedResultsController<Budget *> *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"monthSection" cacheName:nil];
    aFetchedResultsController.delegate = self;

    NSError *error = nil;
    if (![aFetchedResultsController performFetch:&error]) {
        LogError(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }

    _fetchedResultsController = aFetchedResultsController;
    return _fetchedResultsController;
}

然后对于 Tableview Sections 数据源:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    NSInteger count = [sectionInfo numberOfObjects];
    LogDebug(@"Number of Rows: %ld in Section %ld",(long)count, (long)section);
    return [sectionInfo numberOfObjects];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger count = [[self.fetchedResultsController sections] count];
    LogDebug(@"Sections: %ld",(long)count);
    return [[self.fetchedResultsController sections] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    LogDebug(@"STARTED");
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
    return [sectionInfo name];
}

我也试过对数据对象模型进行子类化并将其添加到设置月份部分:

- (NSString *)monthSection {

    @synchronized (self.startTime) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"MMMM"];
        NSString *sectionTitle = [formatter stringFromDate:self.startTime];

        return sectionTitle;
    }

}

现在,当用户 select 是一个 table 视图单元格时,我通过以下方式将预算对象发送到 DetailViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Budget *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        [controller setDetailItem:object];
        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
        controller.navigationItem.leftItemsSupplementBackButton = YES;
    }
}

然后在 DetailViewController 中,我只在 startTime 上使用 setter:

[startDatePicker setDate:detailItem.startTime];
[[AppDelegate instance] saveContext];
[self totalUpFields];

但是一旦用户将月份日期更改为最初创建的日期以外的日期,它就会抛出上述异常。 我是 NSManaged 对象结构的新手,我一直使用托管配置单例。

更改对象内容的方法如下:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}
- (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;

        default:
            return;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] withBudget:anObject];
            break;

        case NSFetchedResultsChangeMove:
            [tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
            break;
    }
}

感谢您的帮助,如果我需要添加其他详细信息,请告诉我。

我认为这是将最后一行移出节的问题。您可以通过将 case NSFetchedResultsChangeMove: 更改为以下内容来解决此问题:

case NSFetchedResultsChangeMove:
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    break;

看起来发生这种情况是因为部分在之前被修改,因此索引路径发生变化并且预期的source/destination可能不再有效。

首先按 sectionNameKeyPath:

排序也很重要
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"monthSection" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO];
[fetchRequest setSortDescriptors:@[sortDescriptor1, sortDescriptor2]];

您需要在核心数据模型上添加 monthSection 作为 属性 并在 inserting/updating 时设置它,而不是您当前在子类中使用的动态方法(否则执行提取时会出现异常)。