Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

NSDocument doesn't autosave last changes

I had noticed an unsettling behaviour about NSDocument some years ago and created FB7392851, but the feedback didn't go forward, so I just updated it and hopefully here or there someone can explain what's going on.

When running a simple document-based app with a text view, what I type before closing the app may be discarded without notice. To reproduce it, you can use the code below, then:

  1. Type "asdf" in the text view.
  2. Wait until the Xcode console logs "saving". You can trigger it by switching to another app and back again.
  3. Type something else in the text view, such as "asdf" on a new line.
  4. Quit the app.
  5. Relaunch the app. The second line has been discarded.

Am I doing something wrong or is this a bug? Is there a workaround?

class ViewController: NSViewController {

    @IBOutlet var textView: NSTextView!

}

class Document: NSDocument {

    private(set) var text = ""

    override class var autosavesInPlace: Bool {
        return true
    }

    override func makeWindowControllers() {
        let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
        let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
        (windowController.contentViewController as? ViewController)?.textView.string = text
        self.addWindowController(windowController)
    }

    override func data(ofType typeName: String) throws -> Data {
        Swift.print("saving")
        text = (windowControllers.first?.contentViewController as? ViewController)?.textView.string ?? ""
        return Data(text.utf8)
    }

    override func read(from data: Data, ofType typeName: String) throws {
        text = String(decoding: data, as: UTF8.self)
        (windowControllers.first?.contentViewController as? ViewController)?.textView.string = text
    }

}

@Nickkk The issue was been tracked, the response provided to your Feedback report FB7392851 was for your to please file a new feedback report if the issue persists and attach diagnostic information.

Given that you're still able to reproduce the issue, could you please post the new Feedback number. That would help with investigating and tracking the issue.

Thanks

I just created another one, FB17662376. Yes, in the previous one I was asked to create a new one, but without giving any reason. To be honest, it can be quite irritating when you guys think that a bug has been solved and ask to verify and if it's not solved, to open a new feedback. It happens regularly. It's still the same exact issue, so why would one have to create a completely new feedback with the same data as before? It comes across as time-consuming bureaucracy.

NSDocument doesn't autosave last changes
 
 
Q