Re: makeDocumentForURL:nil withContentsOfURL:fileURL causing deletion
Re: makeDocumentForURL:nil withContentsOfURL:fileURL causing deletion
- Subject: Re: makeDocumentForURL:nil withContentsOfURL:fileURL causing deletion
- From: Kevin Perry <email@hidden>
- Date: Mon, 10 Feb 2014 10:15:56 -0800
Using -makeDocumentForURL:nil withContentsOfURL:non-nil was originally designed around the idea of reopening a document that was autosaved into ~/Library/Autosave Information after crash recovery or via the post-Lion "Restore" feature. When such documents are closed without saving, it is desired that the file gets deleted.
As such, this is not originally meant for "copying" the contents of an arbitrary document into a new untitled document in the way that you intend.
One way you may be able to work around this is to call [document setAutosavedContentsFileURL:nil] after creating the document in this way. This will disassociate the document from the original file you loaded the data from. You can then force NSDocument to create its own autosaved backing file in ~/Library/Autosave Information with -autosaveWithImplicitCancellability:completionHandler:. However, note that this should probably be done before the document is made visible, because otherwise there will be a period of time when the window will not have the file required to make the window "restorable".
-KP
On Feb 8, 2014, at 11:43 AM, Trygve Inda <email@hidden> wrote:
> I am reading a document in the format of the old version of my app and
> converting it to a new format. The old version is not a document at all but
> is a fixed data file in /Application Support/MyApp/
>
> MyDocument *untitledDoc = [[NSDocumentController sharedDocumentController]
> makeDocumentForURL:nil withContentsOfURL:fileURL
> ofType:kApplicationDocumentType error:&error];
>
> if (untitledDoc)
> {
> [untitledDoc makeWindowControllers];
> [untitledDoc showWindows];
> [untitledDoc setFileURL:nil];
> [[NSDocumentController sharedDocumentController] addDocument:(NSDocument
> *)untitledDoc];
> }
>
>
>
> This works fine and I am able to migrate the data into a real NSDocument
> subclass.
>
> However, if I close the window (thus closing the document) and choose to not
> save, the file package at fileURL is deleted.
>
> Yikes!
>
> How can I completely disassociate the fileURL from the new document that I
> created. I want to treat fileURL as a template or stationary that is only
> read from, and never written to.
>
> [untitledDoc setFileURL:nil] does not seem to work to disassociate the
> document from the source file.
>
> Thanks,
>
> Trygve
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden