0

I have a prototype project of url based documents, where I cache document info to user defaults. User actions tally the change count which I'd like to autosave - once I figure out how, so an explicit close triggers the standard dialog to save first.

Choosing not to save, the window closes - first calling the window's delegate, method - windowShouldClose, as expected. Choosing save, triggers a call to the document's method

override func save(to url: URL, ofType typeName: String, for saveOperation: NSSaveOperationType, completionHandler: @escaping (Error?) -> Void) {
    do {
        try self.write(to: url, ofType: typeName)
        Swift.print("save(to: \(url.absoluteString) ofType: \(typeName)) ")
    } catch let error {
        NSApp.presentError(error)
    }
}

which runs to completion - I get the debug output, ok but I suspect something's wrong as the window delegate method isn't called; a 2nd try to close the window closes without incident.

I'm confused what state is not right that the change clearing didn't reset, allow the doc to close its window after saving? Btw I do not update change count.

slashlos
  • 913
  • 9
  • 17
  • Yes; the documents in my prototype project are urls. I was thinking of a timer to probe for changes then save state using above which I suspect is the problem. – slashlos Jul 26 '17 at 08:08
  • You shouldn't have to call `updateChangeCount(.changeCleared)`, `NSDocument` will do that. Read the documentation of `save(to:ofType:for:completionHandler:)`. Why do you override `save(to:ofType:for:completionHandler:)`? – Willeke Jul 30 '17 at 13:07
  • Yeah you're right re: clearing, but I decided not to bother with counts and write - to defaults, any changes immediately. I override save: to capture state changes to user defaults. The tricky bit is trying to keep the 'document' feel re: future app invocations restore the documents (urls) currently open - but I'm undecided I need to override the document controllers reopen method. – slashlos Jul 30 '17 at 17:39
  • I still don't understand why you have to override `save(to:ofType:for:completionHandler:)`. At least do what the documentation says: "be sure to invoke super". Can't you use the build in state restoration features? – Willeke Jul 31 '17 at 07:38
  • I wasn't aware save: would write to defaults? but I would extend it to also save the document to a plist file, but I'll try super to see what it does, tkx. – slashlos Aug 01 '17 at 20:32
  • Whoa, I pulled the save as suggested; I guess I never needed it, just the write:, thanks! – slashlos Aug 01 '17 at 20:48
  • p.s. but now I'm presented with a save dialog - ugh. I gather I want the call to super save: invoked to maintain state for reopen, but I'm trying to avoid any save dialog as I'm attempting an auto save design. – slashlos Aug 01 '17 at 20:58

0 Answers0