Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
Working on a database program. However, the way this program works, is that the user will only ever have one of this database either open or saved at any time. Now I'm looking at the NSDocument architecture, and a lot of it seems like it would be right for this application, but it's designed for multiple documents, and I don't see anything to change that. Is there a way to limit the document architecture to limit the program to just one document, or am I barking up the wrong tree here?
 
Working on a database program. However, the way this program works, is that the user will only ever have one of this database either open or saved at any time. Now I'm looking at the NSDocument architecture, and a lot of it seems like it would be right for this application, but it's designed for multiple documents, and I don't see anything to change that. Is there a way to limit the document architecture to limit the program to just one document, or am I barking up the wrong tree here?

If NSDocument looks ideal for your uses, consider subclassing NSDocumentController to prevent more than one document being open at a time (look at the methods that support multiple documents and override them).
 
Thanks. However, when I look at this closer, NSDocument may be too much for my needs. The program is basically a single array that's going to be loaded from a file when the app starts, and saved when the app closes. There's not going to be any New, Open or Save entries in the menus. I don't even need the NSUndoManager features of NSDocument. Any other ways to save to a file?
 
Thanks. However, when I look at this closer, NSDocument may be too much for my needs. The program is basically a single array that's going to be loaded from a file when the app starts, and saved when the app closes. There's not going to be any New, Open or Save entries in the menus. I don't even need the NSUndoManager features of NSDocument. Any other ways to save to a file?

Just use fopen, fread and fwrite.
 
Unless you already know the ins-and-outs of NSDocumentController I would say it may be less work to just use a normal app setup.

NSDocumentController doesn't really handle the saving/loading. It gives some basic methods to override so you don't have to mess with the open panel and stuff, but you still need to figure out what exactly is getting loaded and saved.

If you have an NSArray with custom objects, NSKeyedArchiver will probably be the simplest approach.
 
Unless you already know the ins-and-outs of NSDocumentController I would say it may be less work to just use a normal app setup.

NSDocumentController doesn't really handle the saving/loading. It gives some basic methods to override so you don't have to mess with the open panel and stuff, but you still need to figure out what exactly is getting loaded and saved.

If you have an NSArray with custom objects, NSKeyedArchiver will probably be the simplest approach.

That sounds like exactly what I need, but the book I'm learning from only shows using the NSKeyedArchiver in a document-based program, using NSDocument methods to get the information from the archiver to a file. Once I use encodeWithCoder method, how do I finish the step to get the info into a file?
 
That sounds like exactly what I need, but the book I'm learning from only shows using the NSKeyedArchiver in a document-based program, using NSDocument methods to get the information from the archiver to a file. Once I use encodeWithCoder method, how do I finish the step to get the info into a file?

Once you have used NSKeyedArchiver's +archivedDataWithRootObject:, you can use one of NSData's write to file methods (just as you would use one of those methods to read the file). If your database is very large, you might want to consider ways to read it in parts instead of all at once - for this, NSFileHandle would be useful.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.