NSTimer 在同一个线程上调用选择器方法两次?
1)我在viewdidload上创建了一个NSTimer
#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)选择器回调执行一些条件逻辑(视频的播放时间)。问题是每次计时器唤醒时,它都会为同一线程上的同一实例调用选择器方法两次!我怎么知道? NS 日志语句打印线程详细信息并进行调试。
- (void) onTimerKicks:(NSTimer *) timer
{
if (currentPlaybackTime == 10) {
#ifndef NDEBUG
NSLog(@"[%@] ************calling onTimerKicks:onAutoPageTimer from onTimer current playbacktime = %d", [NSThread currentThread], currentPlaybackTime);
#endif
}
}
3)这是定时器一次唤醒的调试
2011-08-30 19:11:51.759 MASLTales[7735:207] ************In 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
有人知道为什么我总是看到这个消息两次吗? 提前致谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上它不是调用选择器两次,而是连续调用选择器,因为在初始化计时器时您已经编写了
repeats:YES];.
但在您的选择器中有一个条件块,该条件块仅在 1 秒内为 true并且您的计时器每半秒触发一次选择器。所以它打印了两次。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.