runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器

Posted qingzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器相关的知识,希望对你有一定的参考价值。

1.

技术分享图片


2、
技术分享图片



  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 @property (nonatomic, strong) dispatch_source_t timer;
  5 @end
  6 
  7 @implementation ViewController
  8 
  9 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 10     [self gcdTimer];
 11 }
 12 
 13 
 14 /**
 15  GCD定时器
 16  */
 17 -(void)gcdTimer{
 18     
 19     
 20     /**
 21       DISPATCH_SOURCE_TYPE_TIMER 定时器
 22       0 描述信息
 23       0 更详细的描述信息
 24       dispatchQueue : 队列决定定时器任务在哪个线程
 25      */
 26 //    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
 27     dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
 28     
 29     /*
 30      DISPATCH_TIME_NOW :起始时间
 31      intervalInSeconds 间隔时间
 32      leewayInSeconds 精准度, 绝对精准0
 33      */
 34     dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
 35     
 36     /*
 37      定时器任务
 38      */
 39     dispatch_source_set_event_handler(timer, ^{
 40         NSLog(@"=== %@", [NSThread currentThread]);
 41     });
 42     dispatch_resume(timer);
 43     
 44     /*
 45      此时 定时器不会工作, 因为定时器有可能被释放掉了,异步线程和主线程都不会执行 ,需要强引用, strong,
 46      GCD定时器是绝对精准的,拖拽tableview或者textview都会执行,NSTimer 分界面追踪模式和默认模式 
 47      */
 48     
 49     self.timer = timer;
 50 }
 51 
 52 -(void)runloopTimer{
 53     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];// 在主线程
 54     
 55     // NSRunLoopCommonModes:会同时执行默认模式(NSDefaultRunLoopMode)和界面追踪模式(UITrackingRunLoopMode)
 56     [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode]; // 等价于 CFRunLoopAddTimer (参数1,参数2,参数3)
 57 }
 58 - (void)runloopTimer22{
 59     NSRunLoop *runloop = [NSRunLoop currentRunLoop];
 60     
 61     /**
 62      默认是NSDefaultRunLoopMode
 63      */
 64     [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
 65     [runloop run];
 66 }
 67 
 68 
 69 -(void)runTimer{
 70     NSLog(@"===%@====",[NSThread currentThread]);
 71 }
 72 
 73 -(void)runLoop{
 74     //主线程runloop
 75     NSRunLoop *mainRunloop = [NSRunLoop mainRunLoop];
 76     NSLog(@"mainRunloop =========== %p", mainRunloop);
 77     //    NSLog(@"mainRunloop 是个啥 === %@", mainRunloop);//打印一坨东西看不明白
 78     
 79     //当前线程runloop
 80     NSRunLoop * currentRunLoop = [NSRunLoop currentRunLoop];
 81     NSLog(@"currentRunLoop ======== %p", currentRunLoop);
 82     
 83     //core
 84     NSLog(@"CFRunLoopGetMain ====== %p", CFRunLoopGetMain());
 85     NSLog(@"CFRunLoopGetCurrent === %p", CFRunLoopGetCurrent());
 86     
 87     //转换
 88     NSLog(@"mainRunloop.getCFRunLoop  === %p", mainRunloop.getCFRunLoop);
 89     NSLog(@"currentRunLoop .getCFRunLoop  === %p", currentRunLoop.getCFRunLoop);
 90     /*
 91      2018-07-06 13:52:44.552563+0800 10 - runloop[12596:484429] mainRunloop =========== 0x6040000a2be0
 92      2018-07-06 13:52:44.552908+0800 10 - runloop[12596:484429] currentRunLoop ======== 0x6040000a2be0
 93      2018-07-06 13:52:44.553043+0800 10 - runloop[12596:484429] CFRunLoopGetMain ====== 0x6000001f3600
 94      2018-07-06 13:52:44.553121+0800 10 - runloop[12596:484429] CFRunLoopGetCurrent === 0x6000001f3600
 95      */
 96 }
 97 
 98 
 99 
100 @end

 





以上是关于runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器的主要内容,如果未能解决你的问题,请参考以下文章

RunLoop

OC内存管理-runloop

CFRunLoop源码分析笔记

CFRunLoop源码分析笔记

iOS之深入解析CFRunloop的多线程隐患

浅谈Runloop