Can I make a program for just one document?

Discussion in 'Mac Programming' started by BadWolf13, Feb 21, 2010.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    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).
     
  3. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #3
    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?
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    Just use fopen, fread and fwrite.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    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.
     
  6. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #6
    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?
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    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.
     

Share This Page