Reputation: 5510
I am trying to figure out how to initiate a method call ever hour while my app is running.
I have created the following method which is started once the ViewController is loaded but the problem is, is that it runs once but then never again..
This is what my code looks like.
#define kMaxAliveTimeSeconds 5.0
// currently only running the query every 5 seconds for testing
- (void)deviceTimer {
if (!isAliveTimer) {
isAliveTimer = [NSTimer scheduledTimerWithTimeInterval:kMaxAliveTimeSeconds
target:self
selector:@selector(deviceIsAlive)
userInfo:nil
repeats:NO];
}
else {
if (fabs([isAliveTimer.fireDate timeIntervalSinceNow]) < kMaxAliveTimeSeconds-0.5) {
[isAliveTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kMaxAliveTimeSeconds]];
}
}
}
- (void)deviceIsAlive {
// do stuff here
}
the only thing is deviceIsAlive is called once but then never again.
Upvotes: 0
Views: 52
Reputation: 2538
Either set repeats:YES in the timer allocation statement (so it repeats automatically, otherwise nstimer invalidates itself after the first time) or in your deviceIsAlive function allocate a new timer.
Upvotes: 2