views:

42

answers:

2

Hello Everyone,

I'm currently using the snippet of code presented below to fire some methods every second. My app is running in the background. The problem is that if the computer wakes up after a sleep period the timer wants to retroactively fire all the methods it has missed. Similar issues come up if the user were to change the System Clock time.

Basically I want to implement the proper timer method that will have my methods called *only every current secon*d. If a second (or minute or hour or day) has passed and for whatever reason the methods weren't called I want my app to just continue from the current moment in time.

Also, can we keep this while using NSTimer?

Thanks!

-(void)start
 {
      NSTimer * timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(tasks:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode];
 }
A: 

To deal with the sleep issue, you can unregister and then reregister your timer whenever the machine goes to sleep and wakes up. See this technical note for information on how to do that. This solution won't work for changing the system time, though.

mipadi
@mipadi. Thanks for the help, but I'm specifically trying to solve the problem using the timer scheduling. Kill two (or more) birds with one stone, if you know what I mean ;)
Eric Brotto
A: 

To handle the big time changes you can use the link UIApplicationSignificantTimeChangeNotification and unregister/reregister your timer.

Joshua Weinberg
@Joshua. Is this not for iOS i.e. the iphone?
Eric Brotto
Ah, I spend too much time in iOS land, the equivalent is `NSSystemClockDidChangeNotification` on desktop.http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/doc/uid/20000188-SW94
Joshua Weinberg