 |
 |
|
 |
07-01-2009, 09:21 AM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
|
Right design/code approach to call a method every N hours
Hi there,
I have a small utility app that's syncing some data from my server. I have a sync method and I would like my app calls this method automatically at certain time on a day.
What is best approach to this? I know I could deploy a timer or something but I don't know if this is the right way of doing this kind of thing...? I want the method to be executed every day at 6AM and 6PM and maybe in future every 4 hours or so...
Any tips and pointers to the right place in ADC documentation would be appreciated.
Thanks,
Jume
|
|
|
07-02-2009, 12:38 AM
|
#2 (permalink)
|
|
Registered Member
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
|
You might want to look at the usage of NSTimer method:
Code:
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
It will fire at a given date, just calculate the date, add the timer to the run loop.
__________________
Visit Me
Writing code is not only about writing instructions to a machine / computer, but also about writing something that could be read, understood, and maintained by others. That's why, I like Cocoa.
|
|
|
07-02-2009, 02:56 AM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
|
Quote:
Originally Posted by mnemonic_fx
You might want to look at the usage of NSTimer method:
Code:
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
It will fire at a given date, just calculate the date, add the timer to the run loop.
|
so is this a good practice to have this timer running at all time and waiting to the right hour to come?
|
|
|
07-02-2009, 03:07 AM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
|
Quote:
Originally Posted by Jume
so is this a good practice to have this timer running at all time and waiting to the right hour to come?
|
I can't see why it's a bad practice.
I observe that there're Time Manager functions inside CoreServices framework, but all of it has been deprecated since Mac OS X 10.4, so they advise to use Carbon Event Loop or NSTimer.
My understanding of NSTimer is it will run based on the schedule of the main application event loop, so I don't think it's something that will consume lots of resources, besides the fact that you're doing this on the desktop.
__________________
Visit Me
Writing code is not only about writing instructions to a machine / computer, but also about writing something that could be read, understood, and maintained by others. That's why, I like Cocoa.
|
|
|
07-02-2009, 03:46 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
|
Ok thanks. I will take a look at above timer function.
I was thinking maybe it's better going with launchd scheduled agent which would run my sync utility app at the right time of the day.
BR
Jume
|
|
|
07-02-2009, 03:49 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
|
Oh, well that might be better, that would mean launchd will launch your app automatically, and do those stuff.
__________________
Visit Me
Writing code is not only about writing instructions to a machine / computer, but also about writing something that could be read, understood, and maintained by others. That's why, I like Cocoa.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 328 |
| 32 members and 296 guests |
| abiz, aceallways, becktoan, Blitfast, brianmethod, develsolutions, dre, Duncan C, Eagle11, entity2light, FetaBoy, firearasi, Gambit, gc2010, harkonian, howcr, iGeorG, JonnyBGoode, JoshuaCaputo, lepetitapps, level4, Mizonnz, pablo_ivan57, pacoo2454, rahulskh, rendezvouscp, shawnsf, skorulis, smasher, toddo35, TunaNugget |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,056
Threads: 38,846
Posts: 170,406
Top Poster: smasher (2,562)
|
| Welcome to our newest member, develsolutions |
|