The save button on the note screen must reliably reflect the result of writing to local storage. After a recent change, the screen has become unstable: in some cases the error message disappears, and in others the success state is shown too early.
NoteEditorViewModel manages the note text and save status.saved only after the write to storage has completed.failed and contain the error text.MainActor.MemoryNoteStore with failOnSave = true.NoteEditorViewModel(store:noteID:initialText:).save() and wait for the asynchronous operation to finish.status.failOnSave = false, call save() again, and wait for completion again.Scenario example:
let store = MemoryNoteStore()
store.failOnSave = true
let viewModel = NoteEditorViewModel(store: store, noteID: "n-1", initialText: "Draft")
await viewModel.save()
// an error is expected here
store.failOnSave = false
await viewModel.save()
// a successful result is expected here only after the write
status remains in an error state with the current message.status transitions to saved only when the write has actually completed.store.savedNotes contains the text of the last successfully saved note under the noteID key.Swift 6@MainActor view model without SwiftUI, task is checked as ordinary asynchronous codesave() may be called sequentially from UI, parallel calls are not required in this exerciseUse line comments to explain the problem, its impact, the fix and how to test it.