On 9/2/12 1:12 PM, Mike Abdullah wrote:
On 9/1/12 10:23 PM, Mike Abdullah wrote:
It seems you're right; this is a side-effect of how NSDocument does
safe-saving. If the file wrappers internally hold onto the URL they were last
written to, that's no good if the file moves out underneath them. To cope
they'd have to be using a bookmark or file reference URL instead. Apple might
well be doing so in 10.8 or a later OS, but if you need to target 10.7, that
doesn't help!

Thanks for the reply! Not sure I understand, are you saying in 10.8 this 
problem is non-existent? I don't see anything in the AppKit release notes 
indicating a change of behavior.

I'd make two changes to your routine:

* Instead override -writeSafelyToURL:…. This is the method that's actually
responsible for doing the special file-handling

Not sure what advantage that would give me generally speaking, but I'm using 
background saving (by returning YES in -canAsynchronouslyWriteToURL:::). It 
appears that -writeSafelyToURL: is being called on the background thread, while 
the completion handler in -saveToURL:… is called on the main thread. I'll have 
to think about the implications of that.

* You probably only want to ask the file wrapper to re-read if the operation
is a regular Save, Save As, or Autosave-in-place. For others, you want to
remain pointing at the original URLs I believe

OK, that sounds like a good idea, although my app doesn't support any save 
operations other than Save and Save As (no versions, no autosaving).

One other question though, what does your writing code look like? Are you
overriding one of the -write… methods? Or implementing -fileWrapperOfType:…
?

Yes, I'm using -fileWrapperOfType::

- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError 
**)outError
{
    NSFileWrapper *dataWrapper = [appData documentFileWrapper];
    [self unblockUserInteraction];

    return dataWrapper;
}

-documentFileWrapper basically prepares the wrappers (updates, replaces or 
removes sub-wrappers as necessary).

Actually, scratch my previous advice, I've just remembered what you should be 
doing instead.

The user is free to move the document at any time. If NSDocument detects this, 
it calls -setFileURL: as part of its work to keep up-to-date with the file on 
disk.

So you need to override -setFileURL: to also ask your file wrapper to re-read 
itself. This will happen if the user moves the document, but *also* after a 
save operation. (I'm pretty certain it's for all save ops, not just Save As).

For example, NSPersistentDocument does this, using -setFileURL: as a cue to 
update the persistent store's URL.

OK, thanks a lot for that tip. Even though I overwrote -setFileURL: for some other task it never occurred to me to look if that's a better place. Feels very natural actually. I find it extremely odd that the requirement to update file wrapper isn't documented anywhere.

Regards
Markus
--
__________________________________________
Markus Spoettl
_______________________________________________

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