Objective c 具有NSManaged对象上下文的TableView节引发异常

Objective c 具有NSManaged对象上下文的TableView节引发异常,objective-c,tableview,nsfetchedresultscontroller,nsmanagedobject,Objective C,Tableview,Nsfetchedresultscontroller,Nsmanagedobject,我最近使用配置单例从自管理对象迁移了一个项目,以使用NSFetchedResultController的NSManaged对象上下文。我试图用基于月份的部分填充TableView,但是用户可以选择一个单元格并更改月份。发生这种情况时,会引发以下异常,并且单元格无法更改或编辑 [error] error: Serious application error. An exception was caught from the delegate of NSFetchedResultsCont

我最近使用配置单例从自管理对象迁移了一个项目,以使用NSFetchedResultController的NSManaged对象上下文。我试图用基于月份的部分填充TableView,但是用户可以选择一个单元格并更改月份。发生这种情况时,会引发以下异常,并且单元格无法更改或编辑

    [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;
}
现在,当用户选择表视图单元格时,我通过以下方式将预算对象发送到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中,我只在开始时使用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;
    }
}
-(void)controllerWillChangeContent:(NSFetchedResultsController*)控制器
{
[self.tableView开始更新];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController*)控制器
{
[self.tableView endUpdates];
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(nsInteger)ChangeType:(NSFetchedResultsChangeType)类型的节索引
{
开关(类型){
案例NSFetchedResultsChangesInsert:
[self.tableView insertSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]带RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]带RowAnimation:UITableViewRowAnimationFade];
打破
违约:
返回;
}
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeObject:(id)一个对象
atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)类型
newindepath:(nsindepath*)newindepath
{
UITableView*tableView=self.tableView;
开关(类型){
案例NSFetchedResultsChangesInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath]带RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath]和RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeUpdate:
[self configureCell:[tableView CellForRowAtIndex Path:Indexath]预算:anObject];
打破
案例NSFetchedResultsChangeMove:
[tableView MoveRowatineXpath:indexPath到indexPath:newIndexPath];
打破
}
}

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

我认为将最后一行移出节时会出现此问题。您可以通过将
案例NSFetchedResultsChangeMove:
更改为以下内容来解决此问题:

case NSFetchedResultsChangeMove:
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    break;
发生这种情况的原因似乎是节在行之前被修改,因此索引路径发生移动,预期的源/目标可能不再有效

同样重要的是,首先按
sectionNameKeyPath

NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"monthSection" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO];
[fetchRequest setSortDescriptors:@[sortDescriptor1, sortDescriptor2]];

您需要将
monthSection
添加为核心数据模型的属性,并在插入/更新时设置它,而不是当前在子类中使用的动态方法(否则,在执行获取时会出现异常).

您是否在与您的
UITableView
相同的视图控制器中实现了
controllerDidChangeContent:
方法?如果是这样的话,你能把这个代码添加到你的问题中吗?谢谢,这肯定有帮助
case NSFetchedResultsChangeMove:
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    break;
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"monthSection" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:NO];
[fetchRequest setSortDescriptors:@[sortDescriptor1, sortDescriptor2]];