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

时间:2022-10-09 19:40:55

My app uses a Core Data managed Table View on which I've implemented with the help of some good folks at cimgf.com a method for re-ordering content. Before doing so, I was able to delete objects easily by using the code below:

我的应用程序使用了核心数据管理的表视图,我在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];
    }
}

After adding the code below, when an object is deleted, the app crashes.

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

- (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];
}

I really don't understand why the app crashes. Can someone give me some tips on how to fix this? Thank you!

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

This is the error I get: * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' *

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

1 个解决方案

#1


1  

Before you call deleteRowsAtIndexPath you need to sync your model data to correspond to the number of items you will have in the table view after the deletion operation, which is n-1 in your case. Although you delete it from your persistent store, the item is probably still cached in the array which count you return in table view's number of rows for section method.

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

#1


1  

Before you call deleteRowsAtIndexPath you need to sync your model data to correspond to the number of items you will have in the table view after the deletion operation, which is n-1 in your case. Although you delete it from your persistent store, the item is probably still cached in the array which count you return in table view's number of rows for section method.

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