background and foreground application with same datafile

Discussion in 'Mac Programming' started by MrFusion, Jul 24, 2010.

  1. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    With "Application is background only" in the .plist of your app, it is possible to specify that an app is background only. Apple calls this an "agent". This can be combined with an icon on the right side in the statusbar (apple bar/menubar) via nsstatusitem.

    I want to make an app that runs continuously in the background. When the user starts (via a menu on the statusitem) the GUI app, it should look and act like a regular app with dock icon and menubar. The background app should also continue running at the same time. This is probably possible by just making two different apps. It is not a document-based app, but it is a core-data app. So the data and the datafile is (should? will? can't?) identical for both apps.

    What would be the best approach to tackle this? Two separate apps (but how would you deal with the datafile issue) or 1 app where the .plist is changed on the fly (or is that not possible?). As always, thank you for your time and answer!
     
  2. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #2
    So you are really asking for a daemon to run in the background and the gui justs hooks into it? What are you doing that requires it to run in the background?
     
  3. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    My program should check with other computers on a regular basis, retrieve and store information and when necessary (depending on the data) report back to the user for further intervention. It should act as independently as possible and bother the user as little as possible. Maybe once a week, a few times tops, the user will have to do something. But it is important not to delay that intervention too much. That would just waste time. Messages will be displayed via growl. On the hand, if the user wants to check the status, (s)he should be able to start the gui itself via the statusbar icon.
     
  4. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    I would probably have the background app handle interacting with the file, and the foreground app communicate with the background app.
     
  5. MrFusion thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #5
    I can try that. Syncing the two datafiles (one per app) is maybe also an option? I just started reading about that, not sure yet how straightforward that is.
     
  6. Cenobite macrumors newbie

    Joined:
    Sep 8, 2009
    Location:
    Germany
    #6
    Use NSDistributedNotificationCenter for your communication between the two applications and the userInfo dictionary for exchanging data. This should work fine, as long as you don't have to deal with huge amounts of data or updates that happen very frequently.
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    While convenient, I would not recommend NSDistributedNotificationCenter for that purpose. For example, it makes no guarantees that it won't drop notifications when heavily loaded.

    Cocoa distributed objects or standard unix IPC methods are better choices.
     

Share This Page