How to invoke a method at particular time asynchronously every day in Mac application?
-
This is my first Mac application. I would like to send emails by comparing the current date and saved date of clients by the user in sqlite only one time- every day depending on the condition, asynchronously.
-
Answer:
Note: I'm assuming that by "asynchronous" you mean "do this whether or not the user is logged in and running the application at 9:30 am". The right way to do this is to register a call with https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man8/launchd.8.html that handles the appropriate work for your application. When and how you do this is somewhat flexible. If you're using a .pkg-type installation script you can register the helper on installation. Or you can register on first run of your application, perhaps after the user has provided sufficient configuration information for the helper app to do it's job. http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html delves in to how to do this in great detail. I recommend reading that. And for a description of plist properties that launchd looks at see this https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist. As a very high-level example, let's assume your helper application can be found in your package under MyPackage.app/Contents/SharedSupport/bin/myhelper. Here's a plist file that would launch it every day at 9:30 am and 9:30 pm on the machine: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <!-- This label needs to be unique to the system --> <key>Label</key> <string>com.mycompany.mypackage.myhelper</string> <!-- Toggle to true to prevent it from running --> <key>Disabled</key> <false/> <!-- This is the full path to the helper application --> <key>Program</key> <string>/Applications/MyPackage.app/Contents/SharedSupport/bin/myhelper</string> <!-- This is an array of string arguments to pass in to the app when it's called --> <key>ProgramArguments</key> <array> <string>--daemon</string> </array> <!-- This works much like cron to state when the job should be run --> <key>StartCalendarInterval</key> <array> <!-- Run at 9:30 am --> <dict> <key>Minute</key> <integer>30></integer> <key>Hour</key> <integer>9</integer> </dict> <!-- Run at 9:30 pm --> <dict> <key>Minute</key> <integer>30></integer> <key>Hour</key> <integer>21</integer> </dict> <array> </dict> </plist> Save that in /Library/LaunchAgents/com.mycompany.mypackage.myhelper.plist on your machine and then type: sudo launchctl load /Library/LaunchAgents/com.mycompany.mypackage.myhelper.plist To register it right away with launchd. On reboot it should register automatically. See the http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/launchctl.1.html#//apple_ref/doc/man/1/launchctl man page for more information on using it to interface with launchd.
Lion at Ask Different Visit the source
Other answers
(Assuming you're using Objective-C/Cocoa) The route I would take: Calculate the amount of time to the event Use https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelector%3awithObject%3aafterDelay%3a (available on NSObject) Include any additional additional logic (such as calls to other methods, or your email logic) in the method indicated in the above method.
zwerdlds
Related Q & A:
- How to access a non static method in an abstract class's static method?Best solution by Stack Overflow
- How to call a method once everyday?Best solution by Stack Overflow
- How to call a function asynchronously in PHP?Best solution by Stack Overflow
- How can I keep my flowers and lavender looking beautiful every day?Best solution by Yahoo! Answers
- What time does West Ryde Video Ezy open every day?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.