title: moveRowAtIndexPath using CoreData in Swift 2
body: <p>After reading more than 10 posts written in Obj-C &amp; Swift.
  I still don't figure out how to reorder the rows in my list using CoreData.</p>
  
  <p>Since I know how to add a new object and how to delete one I tried to merge both together and my <code>moveRowAtIndexPath</code> method became</p>
  
  <pre><code>let prefToMove = mainVC.user.prefs[fromIndexPath.row] as! Pref
  
  var prefs = mainVC.user.prefs.mutableCopy() as! NSMutableOrderedSet
  prefs.addObject(prefToMove)
  
  mainVC.user.prefs = prefs.copy() as! NSOrderedSet
  
  // managed object context saving
  var error: NSError?
  do {
    try managedContext!.save()
  } catch let error1 as NSError {
    error = error1
    print("Could not save: \(error)")
  }
  tableView.insertRowsAtIndexPaths([toIndexPath], withRowAnimation: .None)
  
  prefs = mainVC.user.prefs.mutableCopy() as! NSMutableOrderedSet
  prefs.removeObjectAtIndex(fromIndexPath.row)
  mainVC.user.prefs = prefs.copy() as! NSOrderedSet
  
  managedContext.deleteObject(prefToMove)
  
  // managed object context saving
  do {
    try managedContext.save()
  } catch let error1 as NSError {
    error = error1
    print("Could not save: \(error)")
  }
  
  // Delete the row from the data source
  tableView.deleteRowsAtIndexPaths([fromIndexPath], withRowAnimation: .None)
  
  tableView.reloadData()
  </code></pre>
  
  <p>However, it does not work. The delete part works but the insertion fails.
  Someone can give me any help?</p>