NSTimer calls the selector method twice on the same thread?
1) I created a NSTimer on viewdidload
#ifndef NDEBUG
NSLog(@"************In viewDidLoad method initializng timer [%@]",[NSThread currentThread]);
#endif
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimerKicks:) userInfo:clues repeats:YES];
2) The selector call back performs some conditional logic (Play back time of a video) . The problem is every time the timer wakes up it calls the selector method twice for the same instance on the same thread !!! How do I know ? NS log statements printing thread details and debug.
- (void) onTimerKicks:(NSTimer *) timer
{
if (currentPlaybackTime == 10) {
#ifndef NDEBUG
NSLog(@"[%@] ************calling onTimerKicks:onAutoPageTimer from onTimer current playbacktime = %d", [NSThread currentThread], currentPlaybackTime);
#endif
}
}
3) Here is the debug for one wake up of the timer
2011-08-30 19:11:51.759 MASLTales[7735:207] ************In开发者_StackOverflow社区 viewDidLoad method initializng timer [<NSThread: 0x580f3a0>{name = (null), num = 1}]
2011-08-30 19:12:05.760 MASLTales[7735:207] [<NSThread: 0x580f3a0>{name = (null), num = 1}] ************ccalling onTimerKicks:onAutoPageTimer from onTimer current playbacktime = 10
2011-08-30 19:12:06.260 MASLTales[7735:207] [<NSThread: 0x580f3a0>{name = (null), num = 1}] ************ccalling onTimerKicks:onAutoPageTimer from onTimer current playbacktime = 10
Anybody knows why I see this message twice all the time ? Thanks in advance.
Actually its not calling the selector twice, it is calling selector continously because while initializing timer you have written repeats:YES];.
But in your selector there is a conditional block which is true only for a 1 sec and your timer is firing selector for every half second. So its printing twice.
精彩评论