Validate before opening a document in NSDocument architecture

Validate before opening a document in NSDocument architecture

I have an document based application which formats an XML file.

Writing and reading of document is done in my NSDocument subclass

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError 

but if the file is an invalid XML, my app is simply crashing.

So I implemented:

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename   {     if(safe){open new document using .....makeDocumentWithContentsOfURL:......}      else{present alert}   } 

But there are lot of apparent side effects with this. I have to override couple of other methods:

-(BOOL)writeSafelyToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName     forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError   {       return [self writeToURL:absoluteURL ofType:typeName error:outError];       //return YES;   } 

And this is where the beach-ball cursor appears and eventually the application becomes unresponsive.

Is there a better way to validate the document before opening?

Cocoa - Return information from NSOperation


How do I make a header row with a gradient background in my NSOutlineView?
You should be implementing -readFromURL:ofType:error: such that it does not crash on bad XML.

Animating NSDrawer
This is the routine that you should be doing your validation in.

Using NSOperation for threading creates too many objects
What part of your code is causing the crash, and what kind of crash is it?.
How to convert this Perl Skript (Sipgate API) into Objective C / Cocoa?
Regarding the beachball in -writeSafelyToURL:ofType:forSaveOperation:error:, you're probably creating an infinite loop.

How to get the on-screen location of an NSStatusItem
It is possible that -writeToURL:ofType:error: calls -writeSafelyToURL:.....

How can you read text files?
In any case, this is a very strange way to overload it.

Objective C Structs and Memory Management
What are you trying to achieve here? I don't understand how overloading -application:openFile: is related to either your first problem or the overload you describe..


The best bet is calling this in your application:openFile: method:.
[[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:filename] display:YES error:&error]; 
Using that I haven't had any issues (I'm new to the Document Architecture as well).

Just do your validation before that line and everything should work.. Another thing you might find useful: if you override any of the NSDocument loading/saving methods for different types, be sure to call the super version of each at the end if you don't do the save/load within them.

So for example you can have readFromData:ofType:error: for one type of data but readFromFileWrapper:ofType:error: for another, and both will automatically work as long as you call [super ...] at the end of each if you don't handle the requested type..


Why not simply use the return code??. Straight from the docs for readFromURL:ofType:error:: . Return Value
YES if the document contents could be read; otherwise, NO..

82 out of 100 based on 72 user ratings 622 reviews