Why does NSDocument not care about saving Viewer-role document types

  • I have an NSDocument based app that supports two file types, one as an Editor
    (for the new app version's documents) role and one as a Viewer (for old app
    version documents). Both are based on the same document class which reads both
    but saves only the new version format.

    When -saveDocument: gets called on an old-version document, the framework
    completely ignores that by role definition (Viewer), it can't save the format
    and calls -saveToURL:ofType:::: with the old URL (wrong file extension) and old
    type (can't save that).

    I find it a little odd that something like this hasn't been sorted out long ago,
    so naturally I'm wondering once more today, what am I doing wrong?

    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
  • On 11/06/2012, at 3:14 AM, Markus Spoettl wrote:

    > I have an NSDocument based app that supports two file types, one as an Editor (for the new app version's documents) role and one as a Viewer (for old app version documents). Both are based on the same document class which reads both but saves only the new version format.
    >
    > When -saveDocument: gets called on an old-version document, the framework completely ignores that by role definition (Viewer), it can't save the format and calls -saveToURL:ofType:::: with the old URL (wrong file extension) and old type (can't save that).
    >
    > I find it a little odd that something like this hasn't been sorted out long ago, so naturally I'm wondering once more today, what am I doing wrong?

    I believe the 'role' settings are only used by Core Data (if I'm wrong, I'm sure someone will correct me).

    I've run into the same issue. I've had to override -setFileURL: and at that point "upgrade" the URL to have the appropriate extension so that the modern version of the file is later written out with the right extension. It doesn't overwrite the old one because the extension change is seen as a new filename.

    --Graham
  • On 6/11/12 1:48 AM, Graham Cox wrote:
    > I believe the 'role' settings are only used by Core Data (if I'm wrong, I'm sure
    > someone will correct me).
    >
    > I've run into the same issue. I've had to override -setFileURL: and at that
    > point "upgrade" the URL to have the appropriate extension so that the modern
    > version of the file is later written out with the right extension. It doesn't
    > overwrite the old one because the extension change is seen as a new filename.

    When you -saveDocumentAs:, the role seems to be used to determine whether or not
    to present an accessory view with a file-type selector. With multiple editor
    roles you get the accessory, with only one you don't. So clearly it's being used
    by the framework.

    I worked around it by checking in -saveDocument: if the file type the document
    currently has is one we can actually save. If not, I call -saveDocumentAs: on
    super, which suggests the original file path with the new location. In my app
    the user gets a warning about the format change when opening an old document, so
    all this doesn't come as a surprise.

    I tried using -setFileURL: with a changed file extension but that has the
    disadvantage of potentially overwriting a file that's already using that name
    without any warning (however slim the chances of such a file existing are).

    Regards
    Markus
    --
    __________________________________________
    Markus Spoettl
previous month june 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  
Go to today