title: Inserting/Deleting into UITableView with NSFetchedResultsController
body: <p>I'm relatively new to Swift development and I'm struggling with updating a UITableView with a new entity (CoreData) when using NSFetchedResultsController.</p>
  
  <p>I've searched around on the internet and followed various tutorials, but despite following them, when I convert it to my situation something is going amiss and after numerous hours scratching my head over it, I can't seem to see what is wrong.</p>
  
  <p>I wish to store the data in a tableView, allow swiping left for deletion and have a form for creating new data for the table.  I've had the insertion working, but not the deletion and now have the deletion working, but not the insertion.</p>
  
  <p>I have variables for context and collect my data like so:</p>
  
  <pre><code>func loadData() {
      let request = MyData.fetchRequest() as NSFetchRequest&lt;MyData&gt;
      let sort = NSSortDescriptor(key: #keyPath(MyData.name), ascending: true)
      request.sortDescriptors = [sort]
  
      do {
          myResults = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: #keyPath(MyData.name), cacheName: nil)
          try myResults.performFetch()
  
      } catch let error as NSError {
          print("Could not fetch \(error), \(error.userInfo)")
      }
      myTableView.reloadData()
  }
  </code></pre>
  
  <p>I'm trying to add data (when the user taps a button) like so:</p>
  
  <pre><code>let newData = MyData(entity: MyData.entity(), insertInto: context)
                  newData.name = newName
                  newData.colour = Int16(newDataPicker.selectedRow(inComponent: 0))
                  newData.dataID = dataID
  appDelegate.saveContext()
  loadData()
  </code></pre>
  
  <p>And this worked without a problem, but every time I tried to delete, I would get a crash with the model data and the TableView out of synch. After a lot of research, I've implemented the NSFetchedResultsControllerDelegate, as so:</p>
  
  <pre><code>extension ViewController: NSFetchedResultsControllerDelegate {
      func controllerWillChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
          self.myTableView.beginUpdates()
      }
      func controller(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
          switch type {
          case .delete:
              self.myTableView.deleteRows(at: [indexPath! as IndexPath], with: .fade)
  //        case .insert:
  //            self.myTableView.insertRows(at: [newIndexPath! as IndexPath], with: .fade)
          default:
              break
          }
      }
      func controllerDidChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
          self.myTableView.endUpdates()
      }
  }
  </code></pre>
  
  <p>Which now allows me delete beautifully.  Unfortunately, I now can't add as this crashes my app with the <code>Invalid update</code> error.</p>
  
  <p>I've tried using the NSFetchedResultsControllerDelegate to work for inserts as well, with <code>context.insert()</code> but that causes a crash as well.</p>
  
  <p>Please could someone let me know what stupid mistake I'm making? Many thanks!</p>