title: Crash on UITableView endUpdates when moving last row in section
body: <p>I have an UITableViewController which is backed by an NSFetchedResultsController.</p>
  
  <p>My NSFetchedResultsController put results into two sections based on a boolean.</p>
  
  <p>In a background thread, the datasource is altered such that rows are added or removed.
  I have my background thread's NSManagedObjectContext's merging correctly and this situation work fine for most cases. Rows alter when the data is changed and move between sections with animations.</p>
  
  <p>There is one situation however where my application crashes with an EXC_BAD_ACCESS. 
  The case is when the last row in a section is moved to the other section. (Stack trace below). The crash occurs in objc_msgSend but <a href="http://www.sealiesoftware.com/blog/archive/2008/09/22/objc_explain_So_you_crashed_in_objc_msgSend.html" rel="nofollow noreferrer">the normal debugging tips</a> I use aren't returning anything helpful, I simply receive "Value can't be converted to integer" from <code>gdb</code>.</p>
  
  <p>The NSFetchedResultsControllerDelegate methods get called in the following order:</p>
  
  <pre><code>- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller;
  - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id &lt;NSFetchedResultsSectionInfo&gt;)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type;
       // ^ The parameters specify a deletion of the 1st section
  - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath;
       // ^ The parameters specify a moved row from the 1st section to the 1st section 
  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller;
  </code></pre>
  
  <p>Looking through StackOverflow, many of the people who encounter similar issues are backing their UITableViewController manually using an NSArray or NSMutableArray and they're simply not updating the datasource at the correct time. In this case the NSFetchedResultsController is handling returning the number of rows and sections and is definitely updated by the time the <code>[tableView endUpdates]</code> is called.</p>
  
  <p>Has anyone got any debugging tips, pointers or solutions? 
  <a href="http://iphonedevelopment.blogspot.com/2009/11/i-know-youre-tired-of-hearing-about.html" rel="nofollow noreferrer">This blog post</a> hints at working around a similar problem where a new section is created.</p>
  
  <p>I have two options if I cannot solve this issue as is:</p>
  
  <ol>
  <li>Forgo animation on rows and simply call <code>[tableView reloadData]</code></li>
  <li><a href="https://stackoverflow.com/questions/2055399/uitableview-crash-when-updating-cells-that-change-sections">Switch to using NSArray backed storage for the UITableView</a> and hope for the best</li>
  </ol>
  
  <h2>Update</h2>
  
  <p>I have modified the Apple sample Core Data application "Locations" to use an NSFetchedResultsController directly and reproduced the issue.
  <a href="http://dl.dropbox.com/u/297705/LocationsModified.zip" rel="nofollow noreferrer">I have uploaded the source code for this project</a>. Use that project to reproduce the issue. </p>
  
  <ol>
  <li>Simply click the + button a few times and wait</li>
  <li>Every 5 seconds an item gets moved to another category.</li>
  <li>When the first category is about to be emptied, it crashes.</li>
  </ol>
  
  <p>The crash is sometimes about creating two animations for the same cell as per other StackOverflow questions on the topic. More commonly the crash is as discussed above and below.</p>
  
  <h2>Reference</h2>
  
  <p>The stack trace:</p>
  
  <pre><code>#0  0x31e0afbc in objc_msgSend ()
  #1  0x32c11522 in -[_UITableViewUpdateSupport(Private) _computeRowUpdates] ()
  #2  0x32c10510 in -[_UITableViewUpdateSupport initWithTableView:updateItems:oldRowData:newRowData:oldRowRange:newRowRange:context:] ()
  #3  0x32c0f99e in -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] ()
  #4  0x32c0e66c in -[UITableView endUpdatesWithContext:] ()
  #5  0x000088d6 in -[DraftHistoryController controllerDidChangeContent:] (self=0x3f6f1e0, _cmd=0x377ca29c, controller=0x3f716e0) at /Users/ataylor/Documents/Documents/Programming/iPhone/Drafter/Drafter/Drafter/DraftHistoryController.m:323
  #6  0x3775a892 in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] ()
  </code></pre>
  
  <p>My NSFetchedResultsControllerDelegate methods are the same ones from the Apple sample code:</p>
  
  <pre><code>- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
      // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
      [self.tableView beginUpdates];
  }
  
  
  - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
  
      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];
              // Reloading the section inserts a new row and ensures that titles are updated appropriately.
              [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
              break;
      }
  }
  
  
  - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id &lt;NSFetchedResultsSectionInfo&gt;)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
  
      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 {
      // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
      [self.tableView endUpdates];     // &lt;---- Crash occurs here
  }
  </code></pre>
  
  <p>The relevant UITableViewControllerDelegate methods are as follows:</p>
  
  <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return [[self.fetchedResultsController sections] count];
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      id &lt;NSFetchedResultsSectionInfo&gt; sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
      return [sectionInfo numberOfObjects];
  }
  </code></pre>