Applications are open for the Product College - Apply Now


We're at the beginning of the end. It's been quite a journey. In this section, we'll finish our Notes app functionality by implementing persistence with our CoreDataHelper.

Retrieving Notes

When a user first opens our app, we want to retrieve and display all of the user's existing notes. We can do this by retrieve our existing notes and updating our notes array in ListNotesTableViewController at app launch.

Let's go ahead and implement this with our CoreDataHelper!

In ListNotesTableViewController, update the method viewDidLoad() to retrieve the user's previously existing notes:

override func viewDidLoad() {

    notes = CoreDataHelper.retrieveNotes()

The viewDidLoad() of our ListNotesTableViewController happens roughly when the app first launches. We can use our CoreDataHelper to retrieve the user's previously existing notes and populate our notes array.

Next, we'll also update our unwind segue to retrieve our updated notes.

In ListNotesTableViewController, update the unwind segue method unwindWithSegue(_:) to the following:

@IBAction func unwindWithSegue(_ segue: UIStoryboardSegue) {
    notes = CoreDataHelper.retrieveNotes()

Now, each time the user taps the save or cancel bar button item in DisplayNoteViewController, we update our notes array in ListNotesTableViewController. Our didSet property observer will take care of reloading our table view. In combination, we can be sure our table view data is always synced with our NSManagedObjectContext.

Deleting Notes

We'll also need to update our delete note functionality.

In ListNotesTableViewController, update the table view data source method tableView(_:commitEditingStyle:forRowAtIndexPath:) to use our Core Data helper:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let noteToDelete = notes[indexPath.row]
        CoreDataHelper.delete(note: noteToDelete)

        notes = CoreDataHelper.retrieveNotes()

In the code above, we retrieve the note to be deleted corresponding to the selected index path. Then we use our Core Data helper to delete the selected note. Last we update our notes array to reflect the changes in our NSManagedObjectContext.

Updating DisplayNoteViewController

Last, we'll need to update our functionality in DisplayNoteViewController for creating new notes or modifying existing ones. Once again, let's use our CoreDataHelper to update our code.

In DisplayNoteViewController, update prepare(for:sender:) to use our CoreDataHelper:

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

    switch identifier {
    case "save" where note != nil:
        note?.title = titleTextField.text ?? ""
        note?.content = contentTextView.text ?? ""
        note?.modificationTime = Date()


    case "save" where note == nil:
        let note = CoreDataHelper.newNote()
        note.title = titleTextField.text ?? ""
        note.content = contentTextView.text ?? ""
        note.modificationTime = Date()


    case "cancel":
        print("cancel bar button item tapped")

        print("unexpected segue identifier")

We update our prepare(for:sender:) method to make use of our CoreDataHelper. Since we're retrieving notes in our ListNotesTableViewController unwind segue, we no longer need to reference our segue's destination controller.

Running the App

Wooooooooo! We're done. Phew... I'm just as happy as you are. Congrats, you've just finishing building a fully functioning Notes app with Core Data.

Let your hair down and take your new Notes app for a test run.

You can write notes in a box.
You can write notes with a fox.
You can write notes in a house.
You can write notes with a mouse.

You can write notes here or there.
You can write notes anywhere.

Notes, you can now write.


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more