1

我有一个关于使用 NSTimer 的问题,代码如下,我已经简化了代码:

- (void) mainThreadFun
{
    [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(test) userInfo:nil repeats:YES];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
           [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(test1) userInfo:nil repeats:YES];
    });

}

我发现 mainThread 中的 NSTimer 工作但另一个线程中的 NSTimer 没有工作。为什么会发生这种情况,我该如何解决?

4

1 回答 1

5

您不能在 GCD 队列上使用NSTimer 。NSTimer 需要一个NSRunLoop才能运行,而 GCD 队列没有 NSRunLoop。

如果你想要 GCD 队列的定时器功能,你应该使用dispatch_after()一次性定时器,或者使用dispatch_source来重复定时器。

于 2012-05-30T03:07:22.357 回答