Because we updated the CoreDataManager class in the previous episode, we need to make a few changes in the project.

Updating the Notes View Controller

We changed the name of the managedObjectContext property to mainManagedObjectContext. Open NotesViewController.swift and navigate to the fetchedResultsController property. Change managedObjectContext to mainManagedObjectContext.

NotesViewController.swift

private lazy var fetchedResultsController: NSFetchedResultsController<Note> = {
    ...

    // Create Fetched Results Controller
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest,
                                                              managedObjectContext: self.coreDataManager.mainManagedObjectContext,
                                                              sectionNameKeyPath: nil,
                                                              cacheName: nil)

    ...
}()

We also need to apply this change in the prepare(for:sender:) method.

NotesViewController.swift

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let identifier = segue.identifier else { return }

    switch identifier {
    case Segue.AddNote:
        ...

        // Configure Destination
        destination.managedObjectContext = coreDataManager.mainManagedObjectContext
    case Segue.Note:
        ...
    default:
        break
    }
}

We repeat this change in the tableView(_:commit:forRowAt:) method of the UITableViewDataSource protocol.

NotesViewController.swift

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    ...

    // Delete Note
    coreDataManager.mainManagedObjectContext.delete(note)
}

That's it. Even though we could have kept the name of the managed object context unchanged, the name of the property now clearly reflects the nature and purpose of the managed object context. It's associated with the main queue and it's this managed object context we need to use for any operations related to the user interface. Because remember that the user interface of an application should always be updated on the main thread.