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?