Re: Saving while opening with NSDocument
Re: Saving while opening with NSDocument
- Subject: Re: Saving while opening with NSDocument
- From: Quincey Morris <email@hidden>
- Date: Tue, 18 Mar 2014 00:07:35 -0700
On Mar 11, 2014, at 10:24 , Colas B <email@hidden> wrote:
> I would like to perform `saveDocument:` in the process of opening a document (I call this method in `- (id)initWithContentsOfURL:ofType:error:`.
>
> It does not work because of a deadlock.
Yes, you can’t really expect to use the NSDocument subclass object for something like a save until the document has finished opening.
The easiest way to handle this is probably also the best from a UI point of view, other (unstated by you) requirements aside. What you would do, when you discover that the data needs to be fixed, is fix it in the in-memory representation. Then, invoke ‘updateChangeCount:’ on the document with either the value ‘NSChangeCleared’ or ‘NSChangeReadOtherContents’, but *don’t* try to save the document at this point.
This means the change will actually be committed to disk only when the user really does save the document through one of the normal mechanisms. This has the advantage (apart from solving your deadlock) of not messing with the user’s file without the user’s “permission” (given implicitly when the user causes the file to be saved).
The choice of ‘updateChangeCount:’ constant depends on how you want the process to look to the user. If you really want the fix to be invisible to the user, use ‘NSChangeCleared’. (This is assuming that the fix will dirty the document. If not, you don’t have to bother with this step.) If you want to dirty the document to indicate that a save is needed, then use ‘NSChangeReadOtherContents’. (Yes, I understand your post suggests you want the invisible approach, but in some ways it’s politer to indicate to the user that the file(s) will be changed.)
Of course, you may decide that you really need to fix the data at open time. In that case, the easiest solution is probably to use ‘performSelector’ with a delay to schedule the save soon after the document has finished opening. But in this case you will likely have to deal with situations like the file being on a locked disk.
_______________________________________________
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