美文网首页
RunLoop --- 运行循环

RunLoop --- 运行循环

作者: Cat_uncle | 来源:发表于2017-05-11 16:28 被阅读0次

1.runloop的作用 :

    保证程序不退出

    负责事件的监听,时钟(定时器)/触摸事件/网络事件

    当没有事件发生的时候,会让程序进入休眠状态

    渲染屏幕上的点

2.runloop的对象

    Foundation框架里面有NSRunloop,

    [NSRunLoop currentRunLoop];//获取当前线程的runloop对象

    [NSRunLoop mainRunLoop];//获取主线程的runloop对象

    Core Foundation里面有CFRunloopRef

    CFRunLoopGetCurrent();//获取主线程的runloop对象

    CFRunLoopGetMain();//获取主线程的runloop对象

    其中NSRunloop是基于CFRunloopRef加一层OC的封装。

3.runloop的modes

    1.UIInitializationRunLoopMode,刚启动app调用的mode,完成后不在使用.

    2.GSEventReceiveRunLoopMode,接受系统事件的内部mode,用不到。

   3.NSDefaultRunLoopMode,默认mode,苹果建议将时钟还有网络添加到这个mode

  4.UITrackingRunLoopMode,处理UI的mode,并保证在交互时不受其他mode影响

  ———NSRunLoopCommonModes,是一种占位mode。。。使用这个mode会在需要的时候在NSDefaultRunLoopMode跟UITrackingRunLoopMode来回占位,保证在两种mode中都运行不受影响。

4.runloop处理的事件

   1.CFRunLoopSourceRef 事件输入源,

         source0:用于用户主动触发事件(非系统事件)

         source1:通过内核和其它线程相互发送消息,系统事件

  2.CFRunLoopObserverRef ——runloop的观察者

/**

   runloop的活动观察

     kCFRunLoopEntry = (1UL << 0),进入runloop

     kCFRunLoopBeforeTimers = (1UL << 1),处理timer前

     kCFRunLoopBeforeSources = (1UL << 2),处理source前

    kCFRunLoopBeforeWaiting = (1UL << 5),休眠前

    kCFRunLoopAfterWaiting = (1UL << 6),休眠后

    kCFRunLoopExit = (1UL << 7),退出

    kCFRunLoopAllActivities = 0x0FFFFFFFU 所有事件

*/

    // 创建一个监听者

      CFRunLoopObserverRef observer =  CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities,   YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {

              NSLog(@"%ld",activity);

   });

   // 给runloop添加监听者

    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);

    // 释放runloop

    CFRelease(observer);

3.CFRunLoopTimerRef——基于时间的触发器

NSTimer * timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(updataTimer) userInfo:nil repeats:YES];

使用上面方法创建timer会自动加入runloop,但是模式是默认的NSDefaultRunLoopMode(苹果建议将时钟加入此mode)。所以在于界面交互时timer会停止,故而轮播图常常需要使用下面的方法创建定时器

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updataTimer) userInfo:nil repeats:YES];//不会自动加入runloop,需要手动加入

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

[[NSRunLoop currentRunLoop] run];//这是死循环,故而经常单独写一个子线程

CGD的定时器不受runloop影响

//创建队列

dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

//创建定时器

self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

//设置定时器

dispatch_time_t interval = 1.0 * NSEC_PER_SEC;//时间间隔

dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, interval, 0);

//设置事件

dispatch_source_set_event_handler(self.timer, ^{

       NSLog(@"%@------",[NSThread currentThread]);

});

//启动定时器

dispatch_resume(self.timer);

相关文章

  • RunLoop详解

    RunLoop详解 RunLoop运行循环(死循环) RunLoop模式 NSDefaultRunLoopMode...

  • RunLoop概念与响应者链

    一.RunLoop简介 什么是RunLoop? RunLoop就是运行循环,在程序运行的过程中循环做一些事情,如果...

  • 教你如何轻松搞定 Runloop

    认识 Runloop Runloop 就是运行循环,如果没有 Runloop,程序一运行就会退出,有 Runloo...

  • iOS-Runloop1-Runloop

    一. RunLoop相关 什么是Runloop?顾名思义,Runloop就是运行循环,就是在程序运行过程中循环做一...

  • 【iOS】Runloop

    Runloop概念 运行循环(死循环) Runloop作用 保持程序的持续运行 处理app中的各种事件 节省CPU...

  • RunLoop

    RunLoop简介 RunLoop,就是一个运行循环,通过一个内部的运行循环(Event Loop)对事件或者消息...

  • NSRunLoop

    RunLoop运行逻辑 RunLoop面试题: 1、什么是RunLoop? 答:从字面意思上:运行循环、跑圈。 其...

  • 初探Runloop

    1.runloop是什么? runloop 是一个运行循环(死循环); return UIApplicationM...

  • 简单谈谈RunLoop

    1、RunLoop定义 从字面上看,run是运行,执行的意思,loop是循环的意思,其实RunLoop就是运行循环...

  • RunLoop基础

    RunLoop简介 RunLoop运行循环,在程序运行过程中循环做一些事情.如:定时器(Timer)、Perfor...

网友评论

      本文标题:RunLoop --- 运行循环

      本文链接:https://www.haomeiwen.com/subject/fagotxtx.html