Is Apple iPhone SDK 3.0 CoreDataBooks example broken?

I ran into this issue in my application and reproduced it using Apple CoreDataBooks codebase with 0 code modifications. The problem is when you edit the managed object and save the context, the result is a section with only one line now with no lines, and a new section created with the object appended to it, then you get an invalid update exception. It also depends on the ordering of the strings.

To re-create, follow these steps:

  • Open a new instance of the CoreDataBooks example
  • Edit The Selfish Gene by Richard Dawkins and change the author to Ben. Save this. If you go back to the main view, you will notice that the book is under a new section called Ben.
  • Edit this book again and change the author to Doug. Save this. You will receive the following exception:

    2009-10-01 15:14:29.050 CoreDataBooks[10898:20b] *** Assertion failure in -[UITableView_endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-963.10/UITableView.m:729 2009-10-01 15:14:29.051 CoreDataBooks[10898:20b] Serious application error.  Exception was caught during Core Data change processing: Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (2) 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 (0 inserted, 0 deleted). with userInfo (null)
    
          

I would post code from my own application to make sure I am not missing any required delegation methods, except that in the example above, this is an issue that affects the Apple CoreDataBooks example right off the shelf. The NSFetchedResultsController delegation methods are implemented according to their own examples. Their code doesn't seem to forget to handle the newly created sections as it works in other cases with different section orders.

Any help would be greatly appreciated. The modified code example from the CoreDataBooks example would be great.

+2


source to share


2 answers


How to fix it seems to be to change these lines in the RootViewController table updates section:

    case NSFetchedResultsChangeMove:
  [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 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;

      

in



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

      

for some reason. The problem seems to be related to the removal of the last line in the section; it seems that if you don't add additional insertRows, the UITableView will contrive due to the section that was deleted unexpectedly getting a whole bunch of extra rows.

Took me a watch to figure out that one of them.

+2


source


One recommendation is that you completely uninstall the app from your iPhone and / or reset the Simulator to make sure you don't have leftover corrupted data storage.

After uninstalling the app or resetting the simulator, recompile this app from scratch. This will create a new copy of the application and, more importantly, a new data store.



Try running the test again afterwards to see if you are facing the same problem.

+2


source







All Articles