title: Table View not loading after deleting or inserting core data
body: <p>I am using core data to store events into an agenda app I am making. I have a segment controller that allows the app to sort via date or tags. When I add or delete an event to the table view, nothing is changing in the view and the segment bar breaks, nothin happens. However, I when I restart the app, all the elements are sorted, the segment bar works and all elements are there. Its just when I add or delete elements where it breaks.</p>
  
  <p>heres the code:</p>
  
  <pre><code>import UIKit
  import CoreData
  
  class MainVC: UIViewController, UITableViewDelegate,         UITableViewDataSource, NSFetchedResultsControllerDelegate {
  
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var segControl: UISegmentedControl!
  
      var fetchedResultsController: NSFetchedResultsController&lt;Event&gt;!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          tableView.delegate = self
          tableView.dataSource = self
          attemptFetchRequest()
      }
  
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          attemptFetchRequest()
          tableView.reloadData()
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          if let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as? EventCell {
              configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
              return cell
          }
          return EventCell()
      }
  
      //calling configure cell in this VC too
  
      func configureCell(cell: EventCell, indexPath: NSIndexPath) {
          let event = fetchedResultsController.object(at: indexPath as IndexPath)
          cell.configureCell(event: event)
      }
  
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if let events = fetchedResultsController.fetchedObjects , events.count &gt; 0 {
              let event = events[indexPath.row]
              performSegue(withIdentifier: "DetailsVC", sender: event)
          }
      }
  
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if segue.identifier == "DetailsVC" {
              if let dest = segue.destination as? DetailsVC {
                  if let event = sender as? Event {
                      dest.eventToEdit = event
                  }
              }
          }
      }
  
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          if let sections = fetchedResultsController.sections {
              return sections.count
          }
          return 0
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          if let sections = fetchedResultsController.sections {
              let sectionInfo = sections[section]
              return sectionInfo.numberOfObjects
          }
          return 0
      }
  
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String?{ 
          if let sections = fetchedResultsController.sections {
              let currentSection = sections[section]
              return currentSection.name
          }
      return nil
      }
  
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
          return 126
      }
  
      func attemptFetchRequest() {
          let fetchRequest: NSFetchRequest&lt;Event&gt; = Event.fetchRequest()
          let dateSort = NSSortDescriptor(key: "date", ascending: false) //sort by date
          let tagSort = NSSortDescriptor(key: "tag", ascending: false) //sort by tag
          var key: String!
          if segControl.selectedSegmentIndex == 0 {
              key = "date"
              fetchRequest.sortDescriptors = [dateSort]
          } else {
              key = "tag"
              fetchRequest.sortDescriptors = [tagSort]
          }
          let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: key, cacheName: nil)
          controller.delegate = self
          self.fetchedResultsController = controller
          do {
              try fetchedResultsController.performFetch()
          } catch {
              let err = error as NSError
              print("\(err)")
          }
          tableView.reloadData()
      }
  
      func controllerWillChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
          //tableView.beginUpdates()
      }
  
      func controllerDidChangeContent(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;) {
          tableView.reloadData()
          //tableView.endUpdates()
      }
  
      func controller(_ controller: NSFetchedResultsController&lt;NSFetchRequestResult&gt;, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
          switch(type) {
          case.insert:
              if let indexPath = newIndexPath {
                  tableView.insertRows(at: [indexPath], with: .fade)
              }
              break
          case.delete:
              if let indexPath = indexPath {
                  tableView.deleteRows(at: [indexPath], with: .fade)
              }
              break
          case.update:
              if let indexPath = newIndexPath {
                  if let cell = tableView.cellForRow(at: indexPath) as? EventCell {
                      configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
                  }
              }
              break
          case.move:
              if let indexPath = indexPath {
                  tableView.deleteRows(at: [indexPath], with: .fade)
              }
              if let indexPath = newIndexPath {
                  tableView.insertRows(at: [indexPath], with: .fade)
              }
              break
          }
      }
  
      @IBAction func segControllerChanged(_ sender: Any) {
          attemptFetchRequest()
          tableView.reloadData()
      }
  }
  </code></pre>
  
  <p>Heres my other VC</p>
  
  <pre><code>import UIKit
  import CoreData
  
  class DetailsVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
  
  
  @IBOutlet weak var image: UIImageView!
  
  @IBOutlet weak var titleTextField: CustomTextField!
  
  @IBOutlet weak var locationTextField: CustomTextField!
  
  @IBOutlet weak var DescTextField: CustomTextField!
  
  @IBOutlet weak var datePicker: UIDatePicker!
  
  @IBOutlet weak var tagPicker: UIPickerView!
  
  private var _tags = ["Meeting", "Breakfast/Lunch/Dinner", "Appointment", "Other"]
  
  var tags: [String] {
      return _tags
  }
  
  var eventToEdit: Event?
  
  var imgPicker: UIImagePickerController!
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      titleTextField.delegate = self
      locationTextField.delegate = self
      DescTextField.delegate = self
  
      tagPicker.delegate = self
      tagPicker.dataSource = self
  
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "MMMM dd, YYYY H:mm a"
  
      if eventToEdit != nil {
          loadEventData()
      }
  
      imgPicker = UIImagePickerController()
      imgPicker.delegate = self
  
  }
  
      func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {
      let tag = _tags[row]
      return tag
  
  }
  
  func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
      return _tags.count
  }
  
  func numberOfComponents(in pickerView: UIPickerView) -&gt; Int {
      return 1
  
  }
  
  func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
      //do something
  
  }
  
  @IBAction func saveBtnPressed(_ sender: UIButton) {
  
      var event: Event! //guarenteed to have Event
      let pic = Image(context: context)
      pic.image = image.image
  
      if eventToEdit == nil {
          event = Event(context: context)
      } else {
          event = eventToEdit
      }
  
      if let title = titleTextField.text {
          event.title = title
      }
  
      if let location = locationTextField.text {
          event.location = location
      }
  
      if let desc = DescTextField.text {
          event.detail = desc
      }
  
      event.toImage = pic
  
      event.tag = _tags[tagPicker.selectedRow(inComponent: 0)]
  
  
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "MMMM dd, YYYY HH:mm"
      event.fullDate = datePicker.date as NSDate?
  
  
      dateFormatter.dateFormat = "MMMM dd, YYYY"
      event.date = dateFormatter.string(from: datePicker.date)
  
      dateFormatter.dateFormat = "hh:mm a"
      event.time = dateFormatter.string(from: datePicker.date)
  
      ad.saveContext()
  
      _ = navigationController?.popViewController(animated: true)
  }
  
  func loadEventData() {
  
      if let event = eventToEdit {
          titleTextField.text = event.title
          locationTextField.text = event.location
          DescTextField.text = event.detail
          image.image = event.toImage?.image as? UIImage
  
          if let tag = event.tag {
  
              var i = 0
              repeat {
  
                  let t = _tags[i]
                  if t == tag {
                      tagPicker.selectRow(i, inComponent: 0, animated: false)
                  }
  
                  i += 1
              } while (i &lt; _tags.count)
          }
      }
  
  }
  
  @IBAction func deleteBtnPressed(_ sender: UIBarButtonItem) {
      if eventToEdit != nil {
          context.delete(eventToEdit!)
          ad.saveContext()
      }
  
      _ = navigationController?.popViewController(animated: true)
  }
  
  
  @IBAction func imgBtnPressed(_ sender: UIButton) {
      present(imgPicker, animated: true, completion: nil)
  }
  
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
  
      if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
          image.image = img
      }
      imgPicker.dismiss(animated: true, completion: nil)
  }
  
  override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      self.view.endEditing(true)
  }
  
  func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
      titleTextField.resignFirstResponder()
      locationTextField.resignFirstResponder()
      DescTextField.resignFirstResponder()
  
      return true
  }
  </code></pre>
  
  <p>}</p>
  
  <p>thanks!</p>