title: Crash when inserting row in fetchedResultsController delegate method (iOS)
body: <p>I am using the following method to update my tableView when new entities are created, using the fetchedResultsController delegate:<br></p>
  
  <pre><code>func controller(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
      switch type {
      case .insert:
          //Crash here
          self.tableView.insertRows(at: [newIndexPath!], with: .automatic)
      default:
          print("default")
      }
  }
  </code></pre>
  
  <p>This works, but sometimes I am getting the following crash:</p>
  
  <pre><code> *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. 
  </code></pre>
  
  <p><code>The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). *** First throw call stack:</code></p>
  
  <p>Im am using a fetchedResultsController.  I did some research and the answers claim to update the numberOfRowsInSection, but I have the number of rows in the section set to the number of objects in the fetchedResultController section.  I tried calling reloadData() after &amp; before insertingRows but this does not fix it.  Here is my numberOfRowsInSection method:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      if let sections = fetchedResultsController.sections {
          let currentSection = sections[section]
          return currentSection.numberOfObjects
      }
      return 0
  }
  </code></pre>
  
  <p>And my fetchedResultsController:</p>
  
  <pre><code>lazy var fetchedResultsController: NSFetchedResultsController&lt;Project&gt; = {
  
      let userFetchRequest = NSFetchRequest&lt;NSFetchRequestResult&gt;(entityName: "Project")
      let coreDataStack = CoreDataStack.default()
      let managedContext = coreDataStack?.managedObjectContext
  
      let primarySortDescriptor = NSSortDescriptor(key: "dateCreated", ascending: false)
      userFetchRequest.sortDescriptors = [primarySortDescriptor]
  
          let frc = NSFetchedResultsController(
          fetchRequest: userFetchRequest,
          managedObjectContext: managedContext!,
          sectionNameKeyPath: nil,
          cacheName: nil)
  
      frc.delegate = self
  
      return frc as! NSFetchedResultsController&lt;Project&gt;
  }()
  </code></pre>
  
  <p>//SOLUTION:<br>
  This is what I had to do to fix the issue, as mentioned by Zepar.</p>
  
  <pre><code>func controllerWillChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
      self.tableView.beginUpdates()
  }
  
  func controllerDidChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
      self.tableView.endUpdates()
  }
  </code></pre>