macossavensdocumentdocument-based

When Application quits, NSDocument's isDocumentEdited method always returns false


In a document-based application, any change in a document is signalled by a

[self updateChangeCount: NSChangeDone];

At any time, when debugging, the [document isEdited] returns TRUE, as far as the document has been edited. The doc's window displays "Modified" besides the document's name. When I quit application, my app delegate reviews all opened documents, and check whether they've been edited or not, using this loop:

NSUInteger needsSaving = 0;
while (count--) {
    NSWindow *window = [windows objectAtIndex:count];
    Document *document = [Document documentForWindow:window];
    if (document && [document isDocumentEdited]) needsSaving++;
}

It appears that for any document —modified or not— the [document isDocumentEdited]; always returns FALSE! So that the review change/save process cannot be called. If I check the [window isDocumentEdited], it also returns FALSE despite the fact that "Modified" is written in the title bar of the window.

When the applications quits, the changes are eventually saved (automatically) but without informing the user. This is a very disturbing behaviour!

Any idea of what could explain that the isDocumentEdited boolean is reset to FALSE somewhere before entering this process?

I checked all the code to see if there wasn't any [self updateChangeCount: NSChangeCleared] that could be run somewhere, but it's not the case. I can circumvent the problem using a custom updateChange counting system in my Document subclass, but I would rather use the built-in process.

Using Xcode 10 under Mojave, with a 10.12 target


Solution

  • isDocumentEdited is documented as a "value that indicates whether the document has unsaved changes". The behaviour might be happening if autosavesInPlace is enabled. There won't be unsaved changes at application quit.