解决定时器在主线程不工作问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决定时器在主线程不工作问题相关的知识,希望对你有一定的参考价值。

 

有时候在主线程执行一些操作的时候,定时器会被卡死,比如来回滚动和点击屏幕处理一些主线程事务时,

就是说主界面有UITableView或者UIScrollView,滑动UITableView或者UIScrollView。这个时候NSTimer失效了。

NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(someAction) userInfo:nil repeats:YES];

 

解决方法就是将其加入到UITrackingRunLoopMode模式或NSRunLoopCommonModes模式中。

[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

或者

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

以上是关于解决定时器在主线程不工作问题的主要内容,如果未能解决你的问题,请参考以下文章