2010-03-02 6 views
0

개체를 fetchedresultscontroller에서 다른 개체로 이동하려고합니다.하나의 Core Data UITableView에서 다른 객체로 객체를 이동 하시겠습니까?

UISegmentedControl *segmentedControl = self.navigationItem.titleView; 
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath]; 
NSDate *timeValue = [[managedObject valueForKey:@"timeStamp"] copy]; 

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

NSError *error = nil; 
if (![context save:&error]) { 
    /* 
    Replace this implementation with code to handle the error appropriately. 

    abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
    */ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 
[self.tableView reloadData]; 

fetchedResultsController = [fetchedResultsControllers objectAtIndex:(1-segmentedControl.selectedSegmentIndex)]; 
context = [fetchedResultsController managedObjectContext]; 
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Picked" inManagedObjectContext:context]; 

[newManagedObject setValue:timeValue forKey:@"timeStamp"]; 

error = nil; 
if (![context save:&error]) { 
    /* 
    Replace this implementation with code to handle the error appropriately. 

    abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 
    */ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 

fetchedResultsController = [fetchedResultsControllers objectAtIndex:(1-segmentedControl.selectedSegmentIndex)]; 

}

을하지만 그것은 작동하지 않습니다 : 코드는 다음과 같습니다. 나는 두려워하게한다 :

Serious application error. Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null) 

나는 그것이 일하는 법을 정말로 모른다. 핵심 데이터는 작동하기 어렵습니다.

답변

1

어젯밤에이 문제가 발생했습니다.

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath; 
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type; 
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller; 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller; 

문제는 이런 식입니다 : 내 경우, 나는 NSFetchedResultsControllerDelegate 방법을 구현하기 위해 잊고 있었던 당신이 NSManagedObjectContext에서 객체를 삭제 한 다음 컨텍스트를 저장 한 후을의 UITableView가와 동기화되지 변경 사항을 확인하고 존재하지 않는 항목을 요구합니다.

CoreDataBooks 샘플 코드는 NSFetchedResultsController에서 관리 대상 개체를 추가/삭제하는 것과 함께 이러한 기능을 구현하는 방법을 보여줍니다.

관련 문제