count how many NSPersistentDocument instances are open, and detect document close

  • My app connects to some external sensor hardware.  For simplicity,
    all of the external devices point to the same single
    NSPersistentDocument instance (call it the "live document").

    If more than one document is open, user can work with the documents,
    perform analysis, etc., but is not allowed to connect the external
    sensors.  In this case there is no "live document".

    If there is a "live document", that means that data acquisition is
    happening, and no further documents are allowed to be opened.

    I need to do two things.  First, I need to count the open documents.
    I see -orderedDocuments on NSApplication, and -documents on
    NSDocumentController, both of which return an NSArray that I can
    count.  Is there any reason to prefer one over the other, given that
    I am using NSPersistentDocuments and not simply NSDocuments?

    Second, I need to detect document closing.  The case I'm looking for
    is where multiple documents where open (external sensors illegal),
    and now they've been closed, except for one, so external sensors are
    now legal.  I'm thinking just override -close in my
    NSPersistentDocument subclass, and test for the case where
    openDocumentCount has just become 1.  I don't care that -close is not
    called on app quit, since there will be no more work done anyway.  Is
    this a good approach?

    TIA,
    Hal
  • On Nov 15, 2007, at 11:53 PM, Hal Mueller wrote:

    > I need to do two things.  First, I need to count the open documents.
    > I see -orderedDocuments on NSApplication, and -documents on
    > NSDocumentController, both of which return an NSArray that I can
    > count.  Is there any reason to prefer one over the other, given that
    > I am using NSPersistentDocuments and not simply NSDocuments?
    >
    > Second, I need to detect document closing.  The case I'm looking for
    > is where multiple documents where open (external sensors illegal),
    > and now they've been closed, except for one, so external sensors are
    > now legal.  I'm thinking just override -close in my
    > NSPersistentDocument subclass, and test for the case where
    > openDocumentCount has just become 1.  I don't care that -close is
    > not called on app quit, since there will be no more work done
    > anyway.  Is this a good approach?
    >
    Why not create a subclass of NSDocumentController that manages all
    this -- it's the object responsible for managing the documents, has
    all the relevant information, is told when documents close, and can
    readily message all the documents when state changes...

    mmalc
  • Overriding NSDocumentController's -addDocument: and -removeDocument:
    methods did the trick.  I filed radar://5607356 for enhancement to
    Document-based Applications Overview FAQ.

    Hal
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today