title: Rearranging UITableView with Core Data
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1077568/how-to-implement-re-ordering-of-coredata-records">How to implement re-ordering of CoreData records?</a>  </p>
  </blockquote>
  
  
  
  <p>I'm trying to find a code sample that shows how to handle moving/rearranging cells in a tableView when the cell uses a fetchedResultsController (i.e. in conjunction with Core Data).  I'm getting the moveRowAtIndexPath: call to my data source, but I can't find the right combination of black magic to get the table/data to recognize the change properly.</p>
  
  <p>For example, when I move row 0 to row 2 and then let go, it "looks" correct.  Then I click "Done".  The row (1) that had slid up to fill row 0 still has it's editing mode appearance (minus and move icons), while the other rows below slide back to normal appearance.  If I then scroll down, as row 2 (originally 0, remember?) nears the top, it completely disappears.</p>
  
  <p>WTF.  Do I need to somehow invalidate the fetchedResultsController?  Whenever I set it to nil, I get crashes.  Should I release it instead?  Am I in the weeds?</p>
  
  <p>Here's what I've currently got in there...</p>
  
  <pre><code>- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  
      NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
  
      /*
       Update the links data in response to the move.
       Update the display order indexes within the range of the move.
       */
  
      if (fromIndexPath.section == toIndexPath.section) {
  
          NSInteger start = fromIndexPath.row;
          NSInteger end = toIndexPath.row;
          NSInteger i = 0;
          if (toIndexPath.row &lt; start)
              start = toIndexPath.row;
          if (fromIndexPath.row &gt; end)
              end = fromIndexPath.row;
          for (i = start; i &lt;= end; i++) {
              NSIndexPath *tempPath = [NSIndexPath indexPathForRow:i inSection:toIndexPath.section];
              LinkObj *link = [fetchedResultsController objectAtIndexPath:tempPath];
              //[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:tempPath]];
              link.order = [NSNumber numberWithInteger:i];
              [managedObjectContext refreshObject:link mergeChanges:YES];
              //[managedObjectContext insertObject:link];
          }
  
      }
      // Save the context.
      NSError *error;
      if (![context save:&amp;error]) {
          // Handle the error...
      }
  
  }
  
  - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
  
      // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
      if (self.theTableView != nil)
          [self.theTableView beginUpdates];
  }
  
  - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
      // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
      if (self.theTableView != nil) {
          [self.theTableView endUpdates];
      }
  }
  </code></pre>