当前位置:网站首页>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.
边栏推荐
- 图扑数字孪生青岛城轨,赋能智慧交通低碳发展
- 二、1稀疏sparsearray数组
- 如何使用xilinx的FFT ip
- Biotinyl Cystamine_CAS:128915-82-2_生物素半胱胺
- Unity Shader标准光照模型——高光反射
- VsCode connects to the remote server and modifies the file code
- Knowledge of the day: handwritten deep copy and shallow copy (solves the problem of circular references)
- GadgetInspector原理分析
- Comparison of advantages and disadvantages of VsCode and Sublime editors
- VSCode hides the left activity bar
猜你喜欢
随机推荐
Biotin-PEG4-SS-Alkyne_1260247-54-8_生物素-PEG4-SS-炔烃
2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
IO进程线程->目录IO->day3
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
矩阵键盘
THREEJS导入外部OBJ+MTL后内存优化
网络协议01 - 基础概念
Azide-SS-biotin|CAS:1620523-64-9|生物素-二硫键-叠氮可降解 (cleavable) 的 ADC linke
C#二叉树的遍历方法(通过递归)
Biotinyl Cystamine_CAS:128915-82-2_生物素半胱胺
How to import matlab data into modelsim simulation
独立按键控制led进阶(1)
c语言编程练习
IEEE在指定期刊下搜索相关论文
IO进程线程->文件IO->day2
掌握JESD204B(二)–AD6676的调试
二、1稀疏sparsearray数组
Alamofire源码分析 - POST请求
mysql数据库怎么设置手动提交
图扑软件携手华为云再创合作共赢新局面