views:

129

answers:

3

Hi,

I am writing a document based application on OS X. One feature oft this application will be an "import" function.

This function will read data files from the disk which contain data as raw BLOBs and can be parsed into one oder more documents. For this function I will have to create documents programatically. The main problem is, that NSDocumentController only has the makeDocumentWithContentsOfURL:ofType:error: method for creating a document from existing data. There are several way to create my documents and I'd like to know which one would be the cleanest way.

  1. I could write the data for the documents into separate files on the disk and call makeDocumentWithContentsOfURL:ofType:error: for each of them. This seems rather inelegant and could slow the app down with large amounts of data.
  2. I could create empty documents with the NSDocumentController and fill the data in.
  3. I could create the documents in the import function and add them to the NSDocumentController with the addDocument: method. I don't know if this will work because this method seems to be a hook for subclassing.
  4. I could subclass NSDocumentController an add a makeDocumentFromData:ofType:error: method. I think this would be clean but more complicated.

What do you think?

Best Regards, Chris

+1  A: 

This won't scale to a source document with many blobs. What if a source document would have you create 50,000 documents?

A separate converter application would be better. You could make the extraction of a single result document into an NSOperation subclass, so that you could extract documents in parallel. This application would be able to handle any number of documents, and its UI would be a progress bar and maybe a list of the result documents being extracted (with Reveal buttons next to the ones that have been extracted so far).

You could bundle this application with your main application, bundle it inside your main application (and launch it any time the user tries to open a document in the source format), or make it a wholly-separate product.

Peter Hosey
Maybe blob was a little to much. I should have written "bobs". The number of documents created will be < 20.
insanelygreat
+1  A: 

For cases where it really is appropriate to create multiple documents from one source (i.e., you know you won't create an unreasonable number of them):

2. I could create empty documents with the NSDocumentController and fill the data in.

4. I could subclass NSDocumentController an add a makeDocumentFromData:ofType:error: method. I think this would be clean but more complicated.

I like #2 as the implementation of #4.

Peter Hosey
A: 

If you're targeting Tiger or later, why not use the -[NSDocument readFromData:ofType:error] method and then invoke -[NSDocumentController addDocument:]? Then you're good to go with no subclassing involved :)

To create the data for each object you could get a pointer to the bytes using -[NSData bytes], some pointer arithmetic, and -[NSData initWithBytesNoCopy:length:] and that avoids the need to create temporary files.

caleb
Is [NSDocumentController addDocument:] all I have to do to add an existing Document? From the Apple documentation it seemed like a subclassing hook to me.
insanelygreat
It worked fine this way.
insanelygreat