I have an NSDocument based app that has uses packages do to store a complex structure.

When I open a document, I keep the wrapper around handed to the document in

-readFromFileWrapper:ofType:error:

in order to lazy-load parts of the package when my app needs them. Similarly, I keep the wrapper when saving (which is the same object unless it's a new document that didn't have a wrapper before). And here the trouble starts.

When I try to lazy-load package files after saving by requesting -regularFileContents on the corresponding wrapper that are located somewhere down the wrapper hierarchy, they are still available (meaning the sub-wrapper objects are there) but the content returned is nil.

The documentation states that this can happen when the user moves the file after the wrapper has been created. I suspect that this is a side effect of safe-saving, which writes temporary files and moves them into place when everything worked out correctly.

I'm fixing this by calling -readFromURL:options:error: on the document package's root wrapper in the completionHandler of NSDocument's

-saveToURL:forSaveOperation:completionHandler:

before calling the original completionHandler (I'm pasting the relevant code 
below).

As it's not mentioned in the documentation anywhere (meaning saving packages will invalidate wrappers), it doesn't feel quite right. I've been using this a couple of months now without adverse effects, though.

I'm wondering this is expected behavior and/or if there's a better way to make sure the wrapper points to the right place once saving is complete.

I'm using the 10.7 SDK.

Regards
Markus

- (void)saveToURL:(NSURL *)url ofType:(NSString *)typeName
                     forSaveOperation:(NSSaveOperationType)saveOperation
                    completionHandler:(void (^)(NSError *))completionHandler
{
    [super saveToURL:url ofType:typeName
               forSaveOperation:saveOperation
             completionHandler:^(NSError *error) {

         if (error == nil) {
            [rootFileWrapper readFromURL:url options:0 error:&error];
         }

         completionHandler(error);
    }];
}

_______________________________________________

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