On Sep 16, 2013, at 4:38 AM, Mike Abdullah <mabdul...@karelia.com> wrote:

> 
> On 14 Sep 2013, at 21:14, Brian Clark <ba-cl...@comcast.net> wrote:
> 
>> I'm hoping someone can suggest the correct way to deal with the following 
>> problem.
>> 
>> For an image viewing app i display a file in the usual way in an NSDocument. 
>> setFileURL: is properly called by NSDocument's 
>> _initWithContentsOfURL:ofType:error:.
>> 
>> I now want to display the next file in the folder, and move the old file to 
>> the Trash. To do this I do the necessary file reading tasks and call 
>> setFileURL: with the new file URL. I then move the old file to the trash 
>> (using NSFileManager 's trashItemAtURL:resultingItemURL:error: but using 
>> different methods gives the same result.)
> 
> What are "the necessary file reading tasks"? Rather than call -setFileURL: 
> yourself, have you tried calling -revertToContentsOfURL:ofType:error: 
> instead? The top of the class documentation briefly notes that it should 
> always be used for re-reading documents.

The "necessary tasks" are readFromURL:ofType:error: and the other chores 
associated with setting up the document state for the different image file.

Using -revertToContentsOfURL:ofType:error: instead does not work. After doing 
this NSDocument still seems to treat the old URL as the file's document, and I 
still get presentedItemDidMoveToURL: for the old file URL when it is moved to 
the Trash AFTER I-ve called revertToContentsOfURL:ofType:error:. While 
revertToContentsOfURL:ofType:error: may work correctly for the cases for which 
I guess it was intended (as described by the NSDocument Class Reference), it 
doesn't seem to work for this case, for reasons unknown.

The best work-around I've found is to add a file reference URL to my NSDocument 
subclass, and set it in an override of setFileURL:. Then in an override of 
presentedItemDidMoveToURL: I test if the saved file reference URL is the same 
as the new URL, and do nothing if they're not the same file. That seems to 
work, but it's a bit of a hack, and I'd really like to understand if NSDocument 
is simply broken in this regard, or why setFileURL: does not work and what is 
the correct and reliable way to change the URL for an open document.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to