Can someone please suggest a design approach that would allow my Cocoa app (I'll call it "MyApp" here) to create a schedule to relaunch itself regularly and perform some (long-running) tasks? Below are ideas I've thought of so far. I'm new to Objective-C, Cocoa and Max OS X so I don't know how difficult or feasible each of the options are.
- MyApp programmatically creates an Automator workflow to launch MyApp. MyApp then programmatically creates an iCal repeating event that runs that Automator workflow. I don't know if this is possible but it does sound like a lot of code and prone to error. It also makes it very easy for the user simply to delete the iCal event, which means that MyApp would have to be able to deal with that use case.
- MyApp programmatically creates a launchd item. However, I read that Launchd cannot launch a Cocoa app with a GUI (e.g. "launchd and Cocoa apps?"). This presents a problems in that the user should be able to see a GUI so that he/she can choose to cancel/close the task. I'm guessing that one way around this would be for launchd to run a daemon that then launches MyApp?
- Instead of the daemon opening MyApp, the daemon actually has the logic to run the task itself. I would factor out some of the tasks in MyApp into a framework. MyApp would use this framework. I would create a command line tool that uses the same framework, and launchd would call this command line tool. However, having no GUI would be a problem. Also, the command line tool would need access to the same Core Data repository that MyApp uses, and I think each Core Data repository is private to the application for which it was created?
The schedule must be created automagically for each unique user of the application. For exam开发者_高级运维ple, popping up a message asking the user to create the iCal item would not be acceptable.
If the user uninstalls MyApp, I need the ability for the schedule (or schedules, if more than one user has an "account" in MyApp) to be cleaned up.
Have you actually tried launchd? I'm pretty sure that will work with GUI apps. See here.
精彩评论