• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDocument package saving invalidates wrapper? Options?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument package saving invalidates wrapper? Options?


  • Subject: Re: NSDocument package saving invalidates wrapper? Options?
  • From: Markus Spoettl <email@hidden>
  • Date: Sun, 02 Sep 2012 14:15:12 +0200

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 (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


References: 
 >Re: NSDocument package saving invalidates wrapper? Options? (From: Mike Abdullah <email@hidden>)
 >Re: NSDocument package saving invalidates wrapper? Options? (From: Markus Spoettl <email@hidden>)

  • Prev by Date: Re: NSDocument package saving invalidates wrapper? Options?
  • Next by Date: Re: Network and DarkWake
  • Previous by thread: Re: NSDocument package saving invalidates wrapper? Options?
  • Next by thread: Re: NSDocument package saving invalidates wrapper? Options?
  • Index(es):
    • Date
    • Thread