从Core Data托管表视图中删除对象时出错

时间:2013-02-18 11:39:49

标签: ios objective-c core-data ios6 nsfetchedresultscontroller

我的应用程序使用Core Data托管表视图,我在cimgf.com的一些好人的帮助下实现了一种重新排序内容的方法。 在此之前,我可以使用以下代码轻松删除对象:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView beginUpdates]; // Avoid  NSInternalInconsistencyException

        // Delete the person object that was swiped
        Step *stepToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSLog(@"Deleting (%@)", stepToDelete.name);
        [self.managedObjectContext deleteObject:stepToDelete];
        [self.managedObjectContext save:nil];

        // Delete the (now empty) row on the table
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self performFetch];

        [self.tableView endUpdates];

        [delegate stepChangedOnMaster:self];
    }
}

添加以下代码后,删除对象时,应用程序崩溃。

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toIndexPath:(NSIndexPath *)destinationIndexPath;
{
    NSMutableArray *things = [[__fetchedResultsController fetchedObjects] mutableCopy];

    // Grab the item we're moving.
    NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [things removeObject:thing];

    // Now re-insert it at the destination.
    [things insertObject:thing atIndex:[destinationIndexPath row]];

    // All of the objects are now in their correct order. Update each
    // object's displayOrder field by iterating through the array.
    int i = 0;
    for (NSManagedObject *mo in things)
    {
        NSLog(@"%i - %@", i, [mo valueForKey:@"name"]);
        [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
    }
    things = nil;
    [__managedObjectContext save:nil];
    // Save
      NSError *error = nil;
    if (![__managedObjectContext save:&error])
    {
        NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
        NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
        NSLog(@"%@\n\nDetails: %@", msg, details);
    }

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    NSError *errror = nil;
    if (![__fetchedResultsController performFetch:&errror])
    {
        NSLog(@"Unresolved error %@, %@", errror, [errror userInfo]);
        abort();
    }
    [self.tableView reloadData];
}

我真的不明白应用程序崩溃的原因。有人可以给我一些如何解决这个问题的技巧吗?谢谢!

这是我得到的错误: * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(0)必须相等更新(1)之前该部分中包含的行数,加上或减去从该部分插入或删除的行数(0插入,2删除)和加或减移入或移出的行数部分(0移入,0移出)。 *

1 个答案:

答案 0 :(得分:1)

在致电deleteRowsAtIndexPath之前,您需要同步模型数据,以便与删除操作后表视图中的项目数相对应,在您的情况下为n-1。虽然您从持久性存储中删除它,但该项仍可能缓存在数组中,该数组在表视图的section方法行数中返回。