3

tableview と NSFetchedResultsController を使用するアプリがあります。次のエラーが表示されます: キャッチされていない例外が原因でアプリを終了しています ' NSInternalInconsistencyException', 理由: ' no object at index 2 in section at index 0'

.........エラーが発生したオブジェクト行について.Project は、displayOrder が 1 つの属性であるテーブルです。

- (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