由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'索引2处的索引2处没有对象'

时间:2013-02-21 05:46:49

标签: ios core-data nsfetchedresultscontroller nsmanagedobject nsmanagedobjectcontext

使用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;
}

0 个答案:

没有答案