当前位置:网站首页>GCD timer
GCD timer
2022-07-30 07:35:00 【rhubarb_yellow】
First of all, it should be noted that 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 (source code is available).
Next, let's look at the specific implementation of the CGD timer (divided into the following steps):
- Create timer
// get queuedispatch_queue_t queue = dispatch_get_global_queue(0, 0);// create timerself.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0, queue);Note: The timer (dispatch_source_t type) here is actually an OC object, so it must be strongly referenced.
- Set the start time and interval time of the timer
// Set various attributes of the timer (when to start the task, how long to execute the task)// The time parameter of GCD is nanoseconds (1 second == 10 9 nanoseconds), NSEC_PER_SEC represents 10 9 nanosecondsdispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)); // 3 seconds later than the current timeuint64_t interval = 1.0 * NSEC_PER_SEC; //interval timedispatch_source_set_timer(self.timer, start, interval, 0);- Set callback
// set callbackdispatch_source_set_event_handler(self.timer, ^{NSLog(@"-----TorSinLee-----");});- Start timer
// start timerdispatch_resume(self.timer);- Turn off the timer
//Cancel timerdispatch_cancel(self.timer);It can be seen that GCD's timer implementation is much more complicated than NSTimer, but the time accuracy of GCD's timer is better than NSTimer.
边栏推荐
猜你喜欢
随机推荐
GadgetInspector原理分析
顺序二叉树---实现数组的二叉树前序遍历输出
陕西Biotin-LC_CAS:72040-64-3_N-生物素氨基己酸供应商价格
wsl2设置静态ip(固定ip)static ip
Unity Shader标准光照模型——高光反射
IEEE在指定期刊下搜索相关论文
FPGA parsing B code----serial 1
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
Unity Shader 标准光照模型
二、2队列
图计算在网络安全分析中的应用
独立按键控制led
每日一知识:手写深拷贝和浅拷贝(解决了循环引用的问题)
Vim find character
如何使用xilinx的FFT ip
arthas常用命令
OP 代币和不可转让的 NFT 致力于建立新的数字民主
js advanced study notes (detailed)
基于THREEJS场景中模型局部辉光效果
基于 JupyterLab 插件在 GraphScope 中交互式构图









