It is recommended to use GCD’s timer. GCD's timer is different from NSTimer. NSTimer is affected by RunLoop, but GCD's timer is not affected because RunLoop is also based on GCD.
It’s up to you how to use it, please analyze the specific situation.
Personal best practice is Runloop,新建一个类,类初始化实例之后新建一个线程,监听这个线程的Runloop消息,添加信号源,然后在回调的方法里实现你的需求. 这样可以避免一些NSTimererror or other issues.
The second one is monitoringCADisplayLink,当然,监听CADisplayLink, which also has hidden dangers. It may cause "frame drops" due to heavy workload.
By the way, it’s not easy to implement in the background unless you register 音频服务. You can also study this method:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UIApplication : UIResponder
/*! The system guarantees that it will not wake up your application for a background fetch more
frequently than the interval provided. Set to UIApplicationBackgroundFetchIntervalMinimum to be
woken as frequently as the system desires, or to UIApplicationBackgroundFetchIntervalNever (the
default) to never be woken for a background fetch.
This setter will have no effect unless your application has the "fetch"
UIBackgroundMode. See the UIApplicationDelegate method
`application:performFetchWithCompletionHandler:` for more. */
- (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minimumBackgroundFetchInterval NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
@end
It is recommended to use GCD’s timer. GCD's timer is different from NSTimer. NSTimer is affected by RunLoop, but GCD's timer is not affected because RunLoop is also based on GCD.
Just use
NSTimer
单例,然后加入到主线程runloop
Agent plus GCDj plus singleton
It’s up to you how to use it, please analyze the specific situation.
Personal best practice is
Runloop
,新建一个类,类初始化实例之后新建一个线程,监听这个线程的Runloop
消息,添加信号源,然后在回调的方法里实现你的需求. 这样可以避免一些NSTimer
error or other issues.The second one is monitoring
CADisplayLink
,当然,监听CADisplayLink
, which also has hidden dangers. It may cause "frame drops" due to heavy workload.By the way, it’s not easy to implement in the background unless you register
音频服务
. You can also study this method: