Edit and find notes in a Swift app

Rename notes offline and see how Marfa merges two changes, check a note's links as you type, and find notes by title with a filter the core writes.

  • You'll build three features of Reading List: renaming a note, writing a note's text with links to other notes, and finding notes by their title.
  • You need Reading List as Build an app with Swift leaves it, with its notes Bleak House and Middlemarch, and the Quickstart terminal.
  • You'll have an app that edits notes offline, shows how Marfa settled two changes to one note, says what each link names, and keeps a search between launches.

Marfa's core does the work an app would otherwise write itself: it reads the links in a text and writes and reads filters, offline, by the same rules as the server.

Step 1 of 4: Rename a note

Rename a note

Add this function inside Library:

Library.swift
    func rename(_ note: Item, to title: String) async {
        await write { copy in
            try await copy.items.update(
                note.id, Edit(properties: ["title": .string(title)], baseVersion: note.version))
        }
    }

The Edit carries the version of the note the app read, so the instance can tell whether someone else changed it since. Write safely explains versions from the API's side.

Add an editable row at the end of ContentView.swift, and replace Text(note.title ?? "Untitled") in ContentView with NoteRow(library: library, note: note):

ContentView.swift
struct NoteRow: View {
    let library: Library
    let note: Item
    @State private var title = ""

    var body: some View {
        TextField("Title", text: $title)
            .onSubmit { Task { await library.rename(note, to: title) } }
            .onAppear { title = note.title ?? "" }
            .onChange(of: note.version) { title = note.title ?? "" }
    }
}

The row takes the title again whenever the note's version moves, so a change from elsewhere shows. Run the app, change Middlemarch to Middlemarch, by George Eliot, and press Return. Changes shows Sent.

Next steps

On this page