Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-01-2009, 09:21 AM   #1 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
Send a message via Skype™ to Jume
Default 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
Jume is offline   Reply With Quote
Old 07-02-2009, 12:38 AM   #2 (permalink)
Registered Member
 
mnemonic_fx's Avatar
 
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
Default

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.
mnemonic_fx is offline   Reply With Quote
Old 07-02-2009, 02:56 AM   #3 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
Send a message via Skype™ to Jume
Default

Quote:
Originally Posted by mnemonic_fx View Post
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?
Jume is offline   Reply With Quote
Old 07-02-2009, 03:07 AM   #4 (permalink)
Registered Member
 
mnemonic_fx's Avatar
 
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
Default

Quote:
Originally Posted by Jume View Post
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.
mnemonic_fx is offline   Reply With Quote
Old 07-02-2009, 03:46 AM   #5 (permalink)
Registered Member
 
Jume's Avatar
 
Join Date: Jul 2008
Location: Slovenia, EU
Posts: 254
Send a message via Skype™ to Jume
Default

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
Jume is offline   Reply With Quote
Old 07-02-2009, 03:49 AM   #6 (permalink)
Registered Member
 
mnemonic_fx's Avatar
 
Join Date: Jun 2008
Location: Planet Earth
Posts: 415
Default

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.
mnemonic_fx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,056
Threads: 38,846
Posts: 170,406
Top Poster: smasher (2,562)
Welcome to our newest member, develsolutions
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:33 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0