3

有一个使用 tableview 和 NSFetchedResultsController 的应用程序。我收到错误:由于未捕获的异常而终止应用程序' NSInternalInconsistencyException',原因:' no object at index 2 in section at index 0'

.........对于给出错误的事物对象行。Project 是一个表,其中 displayOrder 是一个属性。

- (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)
    {
        [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
    }

    //[things release], things = nil;

    [__managedObjectContext save:nil];
}

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil) return fetchedResultsController;

    fetchRequest = [[NSFetchRequest alloc] init];
    entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                                                managedObjectContext:__managedObjectContext
                                                                                                  sectionNameKeyPath:nil cacheName:@"ThingsCache"];
    aFetchedResultsController.delegate = self;
    [self setFetchedResultsController:aFetchedResultsController];

    return fetchedResultsController;
}
4

0 回答 0