2013-11-03 3 views
0

NSFetchedResultController를 사용하여 TableView에서 행을 삭제하려고하는데이 작업을 수행하는 방법에 대한 많은 예제를 읽었으며 잘못된 작업을 파악할 수 없습니다. 이건 도움이 필요해. 이것은 삭제하는 내 코드NSFetchedResultController를 사용하여 행을 삭제하는 중

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 1 in section at index 0' 

입니다 :

내가지고있어 오류입니다

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

    NSLog(@"Deleting object at row %d", indexPath.row); 

    // DeleteObject 
    NSManagedObject *objToDelete = (NSManagedObject*)[self.fetchedResultsController objectAtIndexPath:indexPath]; 

    // Delete 
    [self.fetchedResultsController.managedObjectContext deleteObject:objToDelete]; 

    NSError *error = nil; 
    [self.fetchedResultsController.managedObjectContext save:&error]; 
    if(error) 
    { 
     NSLog(@"DeleteRow caused : %@", error); 
    } 
} 

이 내 fetchController

- (NSFetchedResultsController *)fetchedResultsController { 


    AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate]; 
    context = [appdelegate managedObjectContext]; 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Dagbok" inManagedObjectContext:context]; 
    [fetchRequest setEntity:entity]; 


    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
           initWithKey:@"header" ascending:YES]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 
    [fetchRequest setFetchBatchSize:40]; 
    [fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"relationship", nil]]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:context sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    _fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    NSArray *matchingdata = [context executeFetchRequest:fetchRequest error:nil]; 


    return _fetchedResultsController; 

} 

입니다 이것은 내 ObjectChanged

입니다
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 


    UITableView *tableView = _tabell; 

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:(VallaDagbokCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [tableView deleteRowsAtIndexPaths:[NSArray 
               arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray 
               arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
} 

답변

0

가져온 결과 컨트롤러를 느슨하게로드 한 것 같습니다. 따라서 FRC 게터에서 페치 요청이 정확히 언제 실행되는지는 명확하지 않습니다. 그것은 전에 전에 대리자 방법에 의해 행이 삭제 된 것 같습니다.

원래 게으른 로딩 코드 재 삽입하여 FRC 게터를 수리 해보십시오 :

if (_fetchedResultsController != nil) { 
    return _fetchedResultsController; 
} 
관련 문제