zoukankan      html  css  js  c++  java
  • runloop 和 CFRunLoop

    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
  • 相关阅读:
    解决 src/MD2.c:31:20: fatal error: Python.h: No such file or directory安装包错误
    Java 保存对象到文件并恢复 ObjectOutputStream/ObjectInputStream
    Elasticsearch安装配置和测试
    [知识图谱] 环境配置:Java8 + Maven3 + HBase + Titan
    Java8安装配置
    MongoDB 安装、运行、使用、数据恢复
    Java堆空间溢出解决方法 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    服务器重装和配置:Ubuntu16.04 + Anaconda3 + GTX1080驱动 + CUDA8 + cuDNN + 常用工具安装
    [Linux] 输出文件的指定行
    [Linux] sed命令使用之在文件中快速删除/增加指定行
  • 原文地址:https://www.cnblogs.com/qingzZ/p/9283001.html
Copyright © 2011-2022 走看看