NSTimer 在同一个线程上调用选择器方法两次?

发布于 2024-12-02 08:49:40 字数 1323 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-12-09 08:49:40

实际上它不是调用选择器两次,而是连续调用选择器,因为在初始化计时器时您已经编写了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文