gpt4 book ai didi

iphone - NSTimer在其他线程中不起作用

转载 作者:行者123 更新时间:2023-12-03 13:16:10 25 4
gpt4 key购买 nike

我在使用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不能工作。为什么会发生这种情况,我该如何解决?

最佳答案

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

如果要使GCD队列具有计时器功能,则应将 dispatch_after() 用于单次计时器,或将dispatch_source用于重复计时器。

关于iphone - NSTimer在其他线程中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com