How to define a centralized timer for app in iphone?
I need to perform certain tasks (man开发者_Go百科y tasks) at different intervals. How can i implement it with single NStimer instance as individual timers will create overhead?
I am doing this exact thing. in my appdelegate i have a method for the timer and threads (i've removed the threads for simplicity):
-(void)startupThreadsAndTimers{
//updatetime
timer = [NSTimer scheduledTimerWithTimeInterval:(1)
target:self
selector:@selector(updateTime)
userInfo:nil
repeats:YES];
}
and i have a method that the timer triggers
-(void)updateTime{
[[NSNotificationCenter defaultCenter] postNotificationName: @"PULSE" object: nil];
}
in the "awakefromnib" method of the appdelgate class i have this:
[self startupThreads];
Now in the veiwdidload method on the viewcontrollers that need to subscribe to this (to update a clock) i subscribe to the notification centre:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTimeLeft) name:@"PULSE" object:nil];
And i have a method called "updateTimeLeft"
This works really well but i will revisit it to use threads for a bit of performance at a later time.
To have things trigger at different intervals you could change these selectors to parse a time value and have the subscribing view controllers use a modulus to decide what to do.
i.e.
switch (timerValue % 15){
case 1:
[self someMethod];
break;
case 2:
[self someOtherMethod];
break;
}
Hope this helps :)
You can use NSNotificationCenter to define a notification and at particular time it dispatches a notification so other class can be notified and do some task accordingly...
refer this tutorial...
I had to use this in my iPhone App, this is what I did:
#define kUpdateInterval(x) (iPerformanceTime % x == 0)
int iPerformanceTime;
-(void) mPerformanceGameUpdates {
if (iPerformanceTime == 0) { //Initialization
}
if (kUpdateInterval(1)) { //Every 1sec
}
if (kUpdateInterval(2)) { //Every 2sec
}
if (kUpdateInterval(5)) { //Every 5sec
}
if (kUpdateInterval(10)) { //Every 10sec
}
if (kUpdateInterval(15)) { //Every 15sec
}
if (kUpdateInterval(20)) { //Every 20sec
}
if (kUpdateInterval(25)) { //Every 25sec
}
iPerformanceTime ++;
}
//The method below helps you out a lot because it makes sure that your timer
//doesn't start again when it already has, making it screw up your game intervals
-(void) mPerformanceTmrGameUpdatesLoopShouldRun:(BOOL)bGameUpdatesShouldRun {
if (bGameUpdatesShouldRun == TRUE) {
if (bPerformanceGameUpdatesRunning == FALSE) {
tmrPerformanceGameUpdates = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(mPerformanceGameUpdates) userInfo:nil repeats:YES];
bPerformanceGameUpdatesRunning = TRUE;
}
} else if (bGameUpdatesShouldRun == FALSE) {
if (bPerformanceGameUpdatesRunning == TRUE) {
[tmrPerformanceGameUpdates invalidate];
bPerformanceGameUpdatesRunning = FALSE;
}
}
}
-(void) viewDidLoad {
[self mPerformanceTmrGameUpdatesLoopShouldRun:TRUE];
//TRUE makes it run, FALSE makes it stop
}
Just change the update intervals and you'll be good to go!
Define in app delegate and access it elsewhere using the UIApplication sharedApplication. Hope this helps
精彩评论