码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-09 12:42:58      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:orm   bubuko   sele   elf   second   track   handler   loop   userinfo   

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定时器

标签:orm   bubuko   sele   elf   second   track   handler   loop   userinfo   

原文地址:https://www.cnblogs.com/qingzZ/p/9283001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!