2010-03-02 117 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

昨晚我自己遇到了這個問題。就我而言,我已經忘了來實現NSFetchedResultsControllerDelegate方法:

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

的問題是這樣的:你刪除NSManagedObjectContext對象後,然後保存的背景下,UITableView是不同步的變化並且正在詢問不存在的項目。

CoreDataBooks示例代碼顯示瞭如何在您的NSFetchedResultsController中添加/刪除管理對象的同時實現這些功能。

相關問題