2010-03-02 8 views
0

Je suis en train de déplacer un objet d'un extracteur de résultats à un autre. Le code ressemble à ceci:Déplacer un objet d'une donnée UITableView à une autre?

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)]; 

}

Mais cela ne fonctionne pas. J'ai le redouté:

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) 

Je ne sais pas vraiment comment le faire fonctionner. Les données de base sont difficiles à travailler.

Répondre

1

J'ai rencontré ce problème moi-même la nuit dernière. Dans mon cas, je l'avais oublié de mettre en œuvre les méthodes 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; 

Le problème ressemble à ceci: une fois que vous supprimez un objet dans le NSManagedObjectContext, puis enregistrez le contexte, le UITableView est synchronisé avec le changements et demande des articles qui n'existent pas.

L'exemple de code CoreDataBooks montre comment implémenter ces fonctions conjointement avec l'ajout/la suppression d'objets gérés dans votre NSFetchedResultsController.

Questions connexes