NSDocument and the dreaded "The location of the document XXX cannot be determined"
NSDocument and the dreaded "The location of the document XXX cannot be determined"
- Subject: NSDocument and the dreaded "The location of the document XXX cannot be determined"
- From: Martin Wierschin <email@hidden>
- Date: Thu, 1 Apr 2010 13:50:37 -0700
Hello everyone,
I'm banging my head against a problem with my NSDocument subclass. The
first time a document is resaved (eg: a regular NSSaveOperation that
overwrites an existing file) all goes well. If the document remains
open and is later resaved again, Cocoa displays a warning that: The
location of the document “filename” cannot be determined.
I have found that a recent code change provoked the warning dialog. In
my NSDocumentController subclass, instead of doing:
doc = [super openDocumentWithContentsOfFile:filePath
display:isDisplay];
I upgraded to:
doc = [super openDocumentWithContentsOfURL:fileURL display:isDisplay
error:errorPtr];
For obvious reasons the latter is superior, and the only way to get
back an NSError when loading a document. If I switch back to the path-
based API, the Cocoa warning goes away. Why does using a URL instead
of a path cause Cocoa to lose track of the file? The file is not being
renamed/moved in the Finder, or otherwise externally manipulated. The
URL (or path) is at all times correct, whether being passed as a
parameter or inspected via NSDocuments -fileURL and -fileName methods.
I should say that I've overridden my NSDocument's save methods, but
for testing purposes I've tried replacing them with dead-simple
overrides:
* Formatted version of the code below: http://pastebin.com/fjpR2WBf
- (BOOL) writeToURL:(NSURL*)writeURL ofType:(NSString*)type
forSaveOperation:(NSSaveOperationType)saveOp originalContentsURL:
(NSURL*)origURL error:(NSError**)errorPtr
{
return [@"Testing" writeToURL:writeURL atomically:NO
encoding:NSUTF8StringEncoding error:errorPtr];
}
- (void) saveToURL:(NSURL*)url ofType:(NSString*)fileType
forSaveOperation:(NSSaveOperationType)saveOp delegate:(id)del
didSaveSelector:(SEL)delSel contextInfo:(void*)delContext
{
NSAssert( [url isEqual:[self fileURL]], @"only support saving over
the same file" );
NSAssert( nil == del, @"no delegate supported" ); // just for
testing.. making sure that Cocoa doesn't expect a callback
NSAssert( NSSaveOperation == saveOp, @"only regular save
supported" ); // also just for testing.. this is all we handle
NSError* error;
BOOL isWritten = [self writeSafelyToURL:url ofType:fileType
forSaveOperation:saveOp error:&error];
if( isWritten ) [self updateChangeCount:NSChangeCleared];
}
If I don't use -writeSafelyToURL:etc:, and instead write the test
string out in -saveToURL:etc: directly, I also find that Cocoa
properly keeps track of the file. But I'd prefer not to sidestep
whatever safety magic the Cocoa method provides.
Any help on this would be much appreciated; I've spent way too long on
this silly thing already.
~Martin
_______________________________________________
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