NSDocument autosave doesn't show as edited

  • Dear list,

    I have an NSDocument app that implements autosave and versions on Lion. The NSDocument subclass is essentially a text editor. If I create a new document and type something in the text view then the 'Edited' tag appears. I then save the document to disk. After this, typing in the text view no longer triggers the 'Edited' state and hence no versions are autosaved. However, using "Save a Version" works fine, and I can view the versions in the versions browser.

    Does anyone have a clue what I'm missing? As I understand it, if I'm using the default NSDocument undo mechanism (which I believe I am - at least undo/redo works on the editor) then I don't need to manually call -updateChangeCount:.

    What else could I be doing wrong?

    Many thanks for any suggestions,

    Martin
  • Does "Revert to Last Saved" become available after saving and editing? Which NSDocument methods are you overriding?

    It is indeed -updateChangeCount: that controls the 'Edited' state. You might try overriding with a call to super and setting a breakpoint or adding a log to make sure it's being called as expected (with NSChangeDone). That should be getting called by the undo mechanism, but it would be good to verify.

    The only other idea I have is that maybe you need to invoke -[NSTextView breakUndoCoalescing] before saving, like TextEdit does.

    -KP

    On May 23, 2012, at 8:18 AM, Martin Hewitson <martin.hewitson...> wrote:

    > Dear list,
    >
    > I have an NSDocument app that implements autosave and versions on Lion. The NSDocument subclass is essentially a text editor. If I create a new document and type something in the text view then the 'Edited' tag appears. I then save the document to disk. After this, typing in the text view no longer triggers the 'Edited' state and hence no versions are autosaved. However, using "Save a Version" works fine, and I can view the versions in the versions browser.
    >
    > Does anyone have a clue what I'm missing? As I understand it, if I'm using the default NSDocument undo mechanism (which I believe I am - at least undo/redo works on the editor) then I don't need to manually call -updateChangeCount:.
    >
    > What else could I be doing wrong?
    >
    > Many thanks for any suggestions,
    >
    > Martin
  • On May 23, 2012, at 05:30 PM, Kevin Perry wrote:

    > Does "Revert to Last Saved" become available after saving and editing? Which NSDocument methods are you overriding?

    I override:

    -autosavesInPlace
    -readFromURL:ofType:error:
    -writeToURL:ofType:error:
    -saveDocument:

    plus some others (like -printDocument) which I don't think are relevant.

    >
    > It is indeed -updateChangeCount: that controls the 'Edited' state. You might try overriding with a call to super and setting a breakpoint or adding a log to make sure it's being called as expected (with NSChangeDone). That should be getting called by the undo mechanism, but it would be good to verify.

    It's being called only once when the document is loaded. It gets called with NSChangeCleared, which I guess is what you would expect. I've no idea why it isn't called after edits.

    If I return NO from autosavesInPlace, I see the same behaviour. The dirty dot in the red button does not appear after an edit. If I listen for text did change notifications from the text view and call -updateChangeCount in there, then everything works normally.

    >
    > The only other idea I have is that maybe you need to invoke -[NSTextView breakUndoCoalescing] before saving, like TextEdit does.

    I added this, but it doesn't seem to change anything.

    Thanks for your thoughts!

    Martin

    >
    > -KP
    >
    > On May 23, 2012, at 8:18 AM, Martin Hewitson <martin.hewitson...> wrote:
    >
    >> Dear list,
    >>
    >> I have an NSDocument app that implements autosave and versions on Lion. The NSDocument subclass is essentially a text editor. If I create a new document and type something in the text view then the 'Edited' tag appears. I then save the document to disk. After this, typing in the text view no longer triggers the 'Edited' state and hence no versions are autosaved. However, using "Save a Version" works fine, and I can view the versions in the versions browser.
    >>
    >> Does anyone have a clue what I'm missing? As I understand it, if I'm using the default NSDocument undo mechanism (which I believe I am - at least undo/redo works on the editor) then I don't need to manually call -updateChangeCount:.
    >>
    >> What else could I be doing wrong?
    >>
    >> Many thanks for any suggestions,
    >>
    >> Martin
    >

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Martin Hewitson
    Albert-Einstein-Institut
    Max-Planck-Institut fuer
        Gravitationsphysik und Universitaet Hannover
    Callinstr. 38, 30167 Hannover, Germany
    Tel: +49-511-762-17121, Fax: +49-511-762-5861
    E-Mail: <martin.hewitson...>
    WWW: http://www.aei.mpg.de/~hewitson
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • On May 23, 2012, at 9:43 AM, Martin Hewitson <martin.hewitson...> wrote:
    >> It is indeed -updateChangeCount: that controls the 'Edited' state. You might try overriding with a call to super and setting a breakpoint or adding a log to make sure it's being called as expected (with NSChangeDone). That should be getting called by the undo mechanism, but it would be good to verify.
    >
    > It's being called only once when the document is loaded. It gets called with NSChangeCleared, which I guess is what you would expect. I've no idea why it isn't called after edits.
    >
    > If I return NO from autosavesInPlace, I see the same behaviour. The dirty dot in the red button does not appear after an edit. If I listen for text did change notifications from the text view and call -updateChangeCount in there, then everything works normally.

    Perhaps the NSTextView's NSUndoManager isn't the same as the NSDocument's?

    This is supposed to work automatically, but maybe you've done something to prevent this. If its delegate doesn't specify one, the NSTextView searches the responder chain for something that responds to -undoManager. Usually that's the NSWindow. If its delegate doesn't specify one, the window will get its undo manager from [[[self windowController] document] undoManager].

    Compare the pointers of [textView undoManager] and [document undoManager]. I suspect they're different.
  • On May 23, 2012, at 12:43 PM, Martin Hewitson <martin.hewitson...> wrote:

    >
    > On May 23, 2012, at 05:30 PM, Kevin Perry wrote:
    >
    >> Does "Revert to Last Saved" become available after saving and editing? Which NSDocument methods are you overriding?
    >
    > I override:
    >
    > -autosavesInPlace
    > -readFromURL:ofType:error:
    > -writeToURL:ofType:error:
    > -saveDocument:
    >
    > plus some others (like -printDocument) which I don't think are relevant.

    Do you override -undoManager or call -setHasUndoManager:?

    It's probably a good idea to list all NSDocument methods you are overriding, regardless of whether you think they're relevant.

    --Kyle Sluder
  • Kevin, thanks for that. This was exactly the problem. My text view subclass asks its delegate for an undo manager, and I forgot to implement the delegate method in this particular document subclass.

    Many thanks!

    Martin

    On May 23, 2012, at 07:00 PM, Kevin Perry wrote:

    >
    > On May 23, 2012, at 9:43 AM, Martin Hewitson <martin.hewitson...> wrote:
    >>> It is indeed -updateChangeCount: that controls the 'Edited' state. You might try overriding with a call to super and setting a breakpoint or adding a log to make sure it's being called as expected (with NSChangeDone). That should be getting called by the undo mechanism, but it would be good to verify.
    >>
    >> It's being called only once when the document is loaded. It gets called with NSChangeCleared, which I guess is what you would expect. I've no idea why it isn't called after edits.
    >>
    >> If I return NO from autosavesInPlace, I see the same behaviour. The dirty dot in the red button does not appear after an edit. If I listen for text did change notifications from the text view and call -updateChangeCount in there, then everything works normally.
    >
    > Perhaps the NSTextView's NSUndoManager isn't the same as the NSDocument's?
    >
    > This is supposed to work automatically, but maybe you've done something to prevent this. If its delegate doesn't specify one, the NSTextView searches the responder chain for something that responds to -undoManager. Usually that's the NSWindow. If its delegate doesn't specify one, the window will get its undo manager from [[[self windowController] document] undoManager].
    >
    > Compare the pointers of [textView undoManager] and [document undoManager]. I suspect they're different.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Martin Hewitson
    Albert-Einstein-Institut
    Max-Planck-Institut fuer
        Gravitationsphysik und Universitaet Hannover
    Callinstr. 38, 30167 Hannover, Germany
    Tel: +49-511-762-17121, Fax: +49-511-762-5861
    E-Mail: <martin.hewitson...>
    WWW: http://www.aei.mpg.de/~hewitson
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
previous month may 2012 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today