11

Mi aplicación tiene dos barras de pestañas ... Cada una lleva al usuario a un controlador de vista de tabla que le presenta una lista de elementos. La primera vista permite al usuario registrar entradas en la base de datos. La otra pestaña/vista se lee de la base de datos y también presenta esos elementos al usuario; sin embargo, no se realizan actualizaciones en el CoreData/persistant store desde esta segunda vista.Error de CoreData enloqueciéndome ... CoreData: error de aplicación grave. Una excepción capturada del delegado de NSFetchedResultsController

Cuando agrego un nuevo elemento a través del primer controlador de vista, aparece perfectamente en la vista. Sin embargo, tan pronto como toco en la otra barra de pestañas para ver el nuevo elemento que aparece en ese controlador de vista, aparece el siguiente error y el elemento recién agregado no aparece ... Nota: si detengo la aplicación y vuelvo a cargar/vuelva a ejecutarlo y comience tocando la 2ª barra de pestañas; el nuevo elemento se mostrará bien, así sé que el modelo se está actualizando correctamente.

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 
2011-10-20 20:56:15.117 Gtrac[72773:fb03] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null) 

Código de la aplicación de delegado donde se transfiere managedObjectContext a los dos viewControllers.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 


    // get a point to the master database context 
    NSManagedObjectContext *context = [self managedObjectContext]; 
    if (!context) { 
     // Handle the error. 
    } 

    // create tab bar controller and array to hold each of the tab-based view controllers that will appear as icons at bottom of screen 
    tabBarController = [[UITabBarController alloc] init]; 
    NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:5];  


    // 
    // setup first tab bar item 
    // 
    // 
    // alloc the main view controller - the one that will be the first one shown in the navigation control 
    RootViewController *rootViewController = [[RootViewController alloc] initWithTabBar]; 

    // Pass the managed object context to the view controller. 
    rootViewController.managedObjectContext = context; 

    // create the navigation control and stuff the rootcontroller inside it 
    UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 

    // set the master navigation control 
    self.navigationController = aNavigationController; 

    // add the navigaton controller as the first tab for the tab bar 
    [localControllersArray addObject:aNavigationController]; 

    [rootViewController release]; 
    [aNavigationController release];  


    // 
    // setup the other tab bar 
    // 
    // 

    // alloc the view controller 
    vcSimulator *vcSimulatorController = [[vcSimulator alloc] initWithTabBar]; 

    UINavigationController *blocalNavigationController = [[UINavigationController alloc] initWithRootViewController:vcSimulatorController]; 

    // Pass the managed object context to the view controller. 
    vcSimulatorController.managedObjectContext = context; 

    // add this controller to the array of controllers we are building 
    [localControllersArray addObject:blocalNavigationController]; 

    // release these guys, they are safely stored in the array - kill these extra references 
    [blocalNavigationController release]; 
    [vcSimulatorController release]; 


    // 
    // 
    // ok, all the tab bars are in the array - get crackin 
    // 
    // 
    // load up our tab bar controller with the view controllers 
    tabBarController.viewControllers = localControllersArray; 

    // release the array because the tab bar controller now has it 
    [localControllersArray release]; 

    [window addSubview:[tabBarController view]]; 
    [window makeKeyAndVisible]; 

    return YES; 




When I add a new item via the first viewcontroller, it shows up perfectly in the view. However, as soon as I tap on the other tab bar to see the new item appear in that viewcontroller, I get the error listed above, and the newly added item does not appear... Note: if I stop the app and reload/re-run it, and start by tapping the 2nd tabbar, the new item shows up fine, so I know the model is being updated fine. 

Here are the tableview delegate methods from the 2nd view controller. 



- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 

    [self.tableView beginUpdates]; 
} 


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

    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  
    UITableView *tableView = self.tableView; 

    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:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      break; 

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

      break; 
    } 

} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 

} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    [self.tableView endUpdates]; 

} 

Cualquier ayuda que pueda proporcionar sería muy apreciada.

He buscado en este sitio y he encontrado muchas instancias de este error, pero ninguno parece encajar. También he visto referencias inferir que este error que estoy viendo es en realidad un error conocido en el código de Apple ...

puntos de quiebre * * Información actualizada

he vuelto y poner en el código y estoy editando la pregunta original con esta información adicional. Cuando el usuario agrega un nuevo elemento a la base de datos, pasa de la vista raíz a la vista listCourses. La transacción de agregar funciona sin problemas y la lista de ViewView UITableView se actualiza perfectamente.

Cuando hago clic en la otra vista que también lee datos del mismo modelo de datos principal, es viewcontroller ejecuta la siguiente secuencia pero nunca termina agregando el nuevo elemento a la vista de tabla. Aquí está la secuencia que atraviesa.

Simulador VC:

- controllerWillChangeContent which runs... 
     [self.tableView beginUpdates]; 

    - didChangeObject 
     ..with message: NSFetchedResultsChangeUpdate 
     ..which ran: 
     [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 

    - controllerDidChangeContent: 
     [self.tableView endUpdates]; 

El otro viewcontroller que funciona muy bien, pasa a través de esta secuencia inmediatamente después de añadir el registro a la base de datos.

ListCourses VC:

- didChangeSection 
    ...with message: NSFetchedResultsChangeInsert 
...which ran: 
    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 

- didChangeObject 
    ..with message: NSFetchedResultsChangeInsert 
    ..which ran: 
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

¿Por qué el viewcontroller llegar el mensaje NSFetchedResultsChangeInsert pero el otro no lo hace?

Aquí están los métodos delegados del controlador de vista defectuoso.

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 





- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates. 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    [self.tableView beginUpdates]; 
} 


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  


    UITableView *tableView = self.tableView; 

    switch(type) { 

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

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

     case NSFetchedResultsChangeUpdate: 

      //[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] 
        atIndexPath:indexPath]; 
      //[tableView reloadData]; 


      break; 

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

      // Reloading the section inserts a new row and ensures that titles are updated appropriately. 
      // [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 

      break; 
    } 
    NSLog(@"vc>>> about to reload data"); 
    // [self.tableView reloadData]; 

} 


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  

    switch(type) { 

     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 

     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 
    // [self.tableView reloadData]; 

} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates. 
    NSLog(@">>> Entering %s [Line %d] ", __PRETTY_FUNCTION__, __LINE__);  
    [self.tableView endUpdates]; 

} 

Gracias, Phil

Respuesta

20

La cordura UITableView comprobación funciona así:

En la línea de [self.tableView beginUpdates]; el tableView llama a su tableView: numberOfRowsInSection: método de delegado, que parece estar devolviendo 3. En la línea [self.tableView endUpdates]; lo llama nuevamente y parece estar regresando 4.Por lo tanto, tableView espera que inserte 1 fila entre estas dos líneas. De hecho, no se están insertando filas por lo que el tableView falla una aserción. (Puede ver el recuento de filas esperado y real en el mensaje de aserción).

El aumento de 3 a 4 filas filas muestra que su NSFetchedResultsController está notando el elemento central de datos recién insertado correctamente. Lo que hay que hacer es poner un punto de interrupción en el inicio de su controlador: didChangeObject: atIndexPath: forChangeType: método y paso a través de él cuando se cambia a la pestaña de segundo después de la inserción de un elemento. Debería ver el caso NSFetchedResultsChangeInsert: de la sentencia switch que se está ejecutando, pero obviamente esto no está sucediendo.

Con suerte, se puede entender por qué la inserción no está teniendo lugar - de lo contrario volver y hacernos saber lo que realmente vio al entrar a través de este método.

editar para agregar:

OK, por lo que sus métodos de delegado NSFetchedResultsController en el segundo controlador de vista son llamados cuando se cambia a esa pestaña en lugar de inmediatamente cuando se inserta el nuevo elemento en la ficha 1. Esto significa que la segunda el controlador de vista no está viendo la inserción (lo que debería ocurrir inmediatamente) y en realidad está respondiendo a alguna otra notificación de actualización de Core Data más tarde que ocurre cuando se cambia a la pestaña 2. El controlador de resultados recuperados está trabajando con información obsoleta en la línea beginUpdates (hay en realidad 4 elementos en el conjunto de resultados aquí no 3). Para cuando llega a la línea endUpdates, ha actualizado su búsqueda y ha encontrado una inserción inesperada.

métodos delegar la NSFetchedResultsController realmente están diseñados para actualizar la interfaz de usuario en el lugar mientras se están haciendo cambios y vista del controlador es visible. En tu caso, estás haciendo cambios y LUEGO muestra el nuevo controlador de vista. El patrón que realmente debe utilizar es para actualizar la tableview en su método viewWillAppear del controlador 2. Algo así debe hacerlo:

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    NSError *error = nil; 
    [resultsController performFetch:&error]; // Refetch data 
    if (error != nil) { 
     // handle error 
    } 

    [self.tableView reloadData]; 
} 

Esto se asegurará de que cada vez que se cambia a la pestaña 2 que está trabajando con datos recientes del modelo.

+1

Gracias, Robin! Seguiré y te haré saber por la mañana ... ¡Estoy aquí! – phil

+0

Todo, consulte la información actualizada al final de la pregunta original. He proporcionado los métodos de delegación de tabla y el flujo de llamadas que estoy viendo a través de puntos de corte y el depurador. ¿Alguna idea de por qué mis métodos de delegado de mesa vista vcSimulator nunca reciben el mensaje NSFetchedResultsChangeInsert? – phil

+0

respuesta editada arriba. –

Cuestiones relacionadas