美文网首页
RunLoop 运行循环

RunLoop 运行循环

作者: Da雪山 | 来源:发表于2017-08-29 22:49 被阅读12次

[哈雷csdn](http://blog.csdn.net/u011619283/article/details/53433243)

RunLoop 运行循环, 是一个iOS底层的机制

RunLoop的作用:

1.保证当前线程不退出,  如:UIApplicationMain 开启了运行循环, 从而监听用户的输入

2.监听事件:触摸事件(scrollView), 时钟事件(NSTimer), 网络事件

3.每条线程上都会有一个RunLop, 默认是不会循环的,默认没有开启

RunLoop的模式:

Source

Observer  -->CFRunLoop-->CoreFoundation

NSTimer

什么是Source ?

CFRunLoopSourceRef 事件源(输入源)

按照函数的调用栈, Source分为两类: 

CFRunLoopSource0 , 非系统内核事件

CFRunLoopSource1: 系统内核事件


一. RunLoop在NSTimer中的运用

一.默认模式  NSDefaultRunLoopMode

NSTimer, 每间隔一段时间, RunLoop执行一次定时方法

//1.类方法,自动加入到RunLoop的默认模式下

//[NSTimer scheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(timerM) userInfo:nil repeats:YES];

//2.实例方法

NSTimer*timer = [NSTimertimerWithTimeInterval:1target:selfselector:@selector(timerM)userInfo:nilrepeats:YES];

//timer 手动加入到RunLoop中,才能触发timer

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode:NSDefaultRunLoopMode];

//千万不要做耗时操作!!否则卡住UI

-(void)timerM{

static   int  num =0;

//如果出现了耗时操作

NSLog(@"睡一会");

[NSThread sleepForTimeInterval:1.0];

NSLog(@"%@%d",[NSThreadcurrentThread],num++);

}

二. UI模式(用户交互模式)  UITrackingRunLoopMode  (唤醒: 触摸事件)

UI模式的优先级最高, RunLoop会先处理UI模式, UI模式只能被--触摸事件--触发!!!

//解决手势冲突问题 , 将 timer同时,加入到UI模式中, 即可

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode:UITrackingRunLoopMode];

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode:NSDefaultRunLoopMode];

三. 占位模式    NSRunLoopCommonModes

//将timer加入到 占位模式     

//缺点: 如果有耗时操作, UI会卡顿

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode: NSRunLoopCommonModes];

二. NSRunLoop在多线程中的运用

//开启一个子线程, 注意,子线程的RunLoop是默认不开启的, 执行完任务后,会被销毁的, 所以,如果想保证子线程不退出, 需要我们手动去开子线程的RunLoop

NSThread*thread = [[NSThreadalloc]initWithBlock:^{

NSTimer*timer = [NSTimertimerWithTimeInterval:1target:selfselector:@selector(timerM)userInfo:nilrepeats:YES];

[[NSRunLoopcurrentRunLoop]addTimer:timerforMode: NSDefaultRunLoopMode];

//手动开启当前线程的runLoop,--- 相当于开启一个死循环

[[NSRunLoopcurrentRunLoop]run];

NSLog(@"就不会走这里了");

}];

[threadstart]; 

三. RunLoop渲染UI

分析:为什么卡顿?

当tableView  需要加载很多图片时,每一张图片的渲染都会耗时, 相当于一次RunLoop循环中渲染了很多图片, 当滚动时,就会造成卡顿

解决办法: 

- 使用多次RunLoop循环, 每一个循环加载一张图片,就不会卡了

- 需要监听RunLoop, Observer使用CFRunLoop  (CFRunLoop  在CoreFoundation框架中)

四. 后台常驻线程(常用)

http://www.jianshu.com/p/d260d18dd551

参考blog.ibireme.com/2015/05/18/runloop/

相关文章

  • 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/ezeadxtx.html