title: How to get number of entities to update properly
body: <p>I am adding an entity from <code>AddPetViewController</code>, which works fine unless I cancel. When I tap the cancel button, all it does it pop back to <code>MainTableViewController</code> but it adds an extra cell to the top of the top. Now if I select the last cell or try to add another entity(without cancelling) the app crashes.</p>
  
  <p><strong>MainTableViewController.swift</strong></p>
  
  <pre><code>var petCount = 0
  lazy var fetchedResultsController: NSFetchedResultsController&lt;Pet&gt; = {
      let fetchRequest = PersistenceManager.shared.fetchRequest()
      let context = PersistenceManager.shared.context
      let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
      return frc as! NSFetchedResultsController&lt;Pet&gt;
  }()
  
  override func viewDidLoad() {
      super.viewDidLoad()
      navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(pushToAddPetViewController))
      tableView.register(MainTableViewCell.self, forCellReuseIdentifier: "cell")
  }
  
  override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      do {
          try fetchedResultsController.performFetch()
          petCount = fetchedResultsController.fetchedObjects!.count
          tableView.reloadData()
      } catch let err {
          print(err)
      }
  }
  
  @objc func pushToAddPetViewController(sender: UIBarButtonItem) {
      let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AddPetViewController") as! AddPetViewController
      self.navigationController?.pushViewController(viewController, animated: true)
  }
  </code></pre>
  
  <p><strong>AddPetViewController.swift</strong></p>
  
  <pre><code>let pet = Pet(context: PersistenceManager.shared.context)
  
  @objc func cancel() {
      self.navigationController?.popViewController(animated: true)
  }
  
  @objc func saveContext() {
      pet.name = nameTF.text
      pet.isBoy = isBoy!
      pet.isDog = isDog!
      pet.breed = breedTF.text
      pet.isVaccinated = isVaccinated!
      pet.weight = weightTF.text
      PersistenceManager.shared.saveContext()
      self.navigationController?.popViewController(animated: true)
  }
  </code></pre>
  
  <p>Both buttons work as they should but a new cell gets added no matter what I do. It should also mention that the new cell added when tapping <code>cancel()</code>, the blank cell doesn't exist if I reopen the app.</p>