title: Assertion failure UITableView: delete object which a relationship to another object
body: <p>I try to delete a present which is assosiated to a person. Now when run the programm and try to delete one present object an error occurs:</p>
  
  <p>HappyBirthdayMom[7907:369895] <strong>* Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Rows_In_Section:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3900.12.16/UITableView.m:2406
  2019-12-13 14:34:55.941744+0100 HappyBirthdayMom[7907:369895] *</strong> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'</p>
  
  <p>I don't really get how to solve this problem - the count of the giftArray should return the number of rows. </p>
  
  <pre><code>
  import UIKit
  import CoreData
  
  class PresentViewController: UITableViewController {
  
      @IBOutlet weak var deleteButton: UIBarButtonItem!
  
      var managedContext : NSManagedObjectContext?
  
      var person: Person?
  
      var giftArray: [Geschenk] {
          return person?.geschenke?.allObjects as? [Geschenk] ?? [Geschenk]()
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view.
      }
  
  
      // MARK: - Table View
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return giftArray.count
  
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
  
          let cell = tableView.dequeueReusableCell(withIdentifier: "GeschenkCell", for: indexPath)
  
          let present = giftArray[indexPath.row]
  
          cell.textLabel?.text = present.title
  
          //Tenary operator ==&gt;
          // value = condidtion ? valueIfTrue : valueIfFalse
          cell.accessoryType = present.isOwned ? .checkmark : .none
  
          return cell
      }
  
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          //print(itemArray[indexPath.row])
  
  
          giftArray[indexPath.row].isOwned = !giftArray[indexPath.row].isOwned
  
          saveGift()
  
          tableView.deselectRow(at: indexPath, animated: true)
      }
  
      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
          if editingStyle == .delete {
              managedContext!.delete(giftArray[indexPath.row] as NSManagedObject)
              tableView.deleteRows(at: [indexPath], with: .fade)
  
              do {
                  try managedContext!.save()
              } catch {
                  ("Error delete gift \(error)")
              }
              self.reloadPerson()
          }
      }
  
      // MARK: - Actions
  
      @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
          var textField = UITextField()
  
          let alert = UIAlertController(title: "Neues Geschenk hinzufügen", message: "", preferredStyle: .alert)
  
          let action = UIAlertAction(title: "Hinzufügen", style: .default) { (action) in
  
              let newGift = Geschenk(context: self.managedContext!)
              newGift.title = textField.text!
              newGift.isOwned = false
              newGift.owner = self.person
  
              self.saveGift()
          }
  
          let actionCancel = UIAlertAction(title: "Abbrechen", style: .cancel) {(action) -&gt; Void in
  
          }
  
          alert.addTextField{ (alertTextfield) in
              alertTextfield.placeholder = "Neues Geschenk hinzufügen"
              textField = alertTextfield
          }
  
          alert.addAction(action)
          alert.addAction(actionCancel)
  
          present(alert, animated: true, completion: nil)
      }
  
      @IBAction func editButtonPressed(_ sender: UIBarButtonItem) {
  
          self.tableView.isEditing = !self.tableView.isEditing
          sender.title = (self.tableView.isEditing) ? "Erledigt" : "Löschen"
  
      }
  
  
      @IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
  
          self.dismiss(animated: true, completion: nil)
      }
  
  
      // MARK: - Data Manipulation
  
      func saveGift() {
          do {
              try managedContext?.save()
  
          }catch {
              ("Error saving Gift \(error)")
          }
  
          self.reloadPerson()
          self.tableView.reloadData()
      }
  
      func reloadPerson() {
          self.person = self.managedContext?.object(with: self.person!.objectID) as? Person
      }
  }
  </code></pre>