iOS关于RunLoop和Timer
RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在。RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop。永远不要手动创建一个runloop,它是跟随着每个线程的。一个RunLoop接收两种source的事件:input source和timer source。同时必须知道的是,input source,runloop是异步交付的,而timer source是同步交付的。每个runloop都有一个RunLoop Modes,代表它以何种方式执行。
我们为什么从来没有感觉到runloop的存在呢,是因为当程序启动,系统默认帮我们启动了一个主线程的runloop,并且一直在运行,直到程序退出。而用户创建的子线程,runloop是需要手动启动的,所以在线程里启动timer或者调用performSelector: withObject:afterDelay:inModes: 是需要启动runloop的。在后面我会介绍到怎么启动。
关于input source :一般来说,input source基本我们用的就是下面几种方式调用的:
Methods |
Description |
performSelectorOnMainThread: withObject: performSelectorOnMainThread: withObject: |
Performs the specified selector on the application’s main thread during that thread’s next run loop cycle. These methods give you the option of blocking the current thread until the selector is performed. |
performSelector: onThread:withObject: performSelector: onThread:withObject: |
Performs the specified selector on any thread for which you have an NSThread object. These methods give you the option of blocking the current thread until the selector is performed. |
performSelector: withObject: afterDelay: performSelector: withObject: |
Performs the specified selector on the current thread during the next run loop cycle and after an optional delay period. Because it waits until the next run loop cycle to perform the selector, these methods provide an automatic mini delay from the currently executing code. Multiple queued selectors are performed one after another in the order they were queued. |
cancelPreviousPerformRequestsWithTarget: cancelPreviousPerformRequestsWithTarget: |
Lets you cancel a message sent to the current thread using the performSelector: withObject: afterDelay: or performSelector: withObject: afterDelay:inModes: method. |
这是系统帮我们封装了一层,非常容易调用。用户可以创建自己的Port-based input sources,用来监听某个端口的事件跟其他线程通信:
- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode; - (void)removePort:(NSPort *)aPort forMode:(NSString *)mode;
就是利用NSPort对象进行的消息传递和delegate,我们一般用不了,有兴趣可以看看NSPort的介绍。
关于timer source:就是我们平时用到的NSTimer了。
至于我们什么时候需要用到runloop呢,主要是下面几种情况:
1.需要用到NSPort或者其他input source跟其他线程通信。
2.在线程启动timer。
3.在线程里调用performSelector...这类函数去调用。
下面我简单用一个例子怎么在线程里启动timer或者performSelector...如下:
-(void)testMain { //开启一个测试子线程 [NSThread detachNewThreadSelector:@selector(threadMethod) toTarget:self withObject:nil]; } -(void)threadMethod { //没用的timer //NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:.2 target:self selector:@selector(timerDone) userInfo:nil repeats:YES]; //真正启动了timer NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:.2target:selfselector:@selector(timerDone) userInfo:nilrepeats:YES]; [[NSRunLoopcurrentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; [[NSRunLoopcurrentRunLoop] run]; //同理,调用performSelector也一样
//[self performSelector:@selector(timerDone) withObject:nil afterDelay:.2];
//[[NSRunLoop currentRunLoop] run]; } -(void)timerDone { NSLog(@"Timer Run"); }
NStimer,几乎每个做iOS开发的程序员都用过,但是有一个关于Timer的介绍估计很多人都不知道:timer是不一定准时的,是有可能被delay的,每次间隔的时间是不一定一样的。
A repeating timer reschedules itself automatically based on the scheduled firing time, not the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so much that it misses one or more of the scheduled firing times, the timer is fired only once for the missed time period. After firing for the missed period, the timer is rescheduled for the next scheduled firing time.
简单解读一下:就是说一个repeat的timer,它在创建的时候就把每次的执行时间算好了,而不是真正启动的时候才计算下次的执行时间。举个例子,假如一个timer在一个特定的时间t激活,然后以间隔5秒的时间重复执行。那么它的执行操作的时间就应该为t, t+5, t+10,... 假如它被延迟了,例如实际上timer在t+2的时候才启动,那么它下次执行的时间还是t+5,而并不是t+2+5,如果很不幸地在t+5还没有启动,那么它理应该在t执行的操作就跟下一次t+5的操作合为一个了。至于为什么会延迟呢,这就跟当前线程的操作有关,因为timer是同步交付的,所以假如当前线程在执行很复杂的运算时,那必须等待运算的完成才能调用timer,这就导致了timer的延迟。
我们就用一个例子来看看效果吧,代码为:
//这里创建timer以每隔1秒执行
[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(timerDone) userInfo:nil repeats:YES];
//这里在第3秒的时候模拟一个复杂运算
[self performSelector:@selector(busyDone) withObject:nil afterDelay:]; -(void)busyDone
{
//这里模拟线程复杂的运算
for(NSInteger i = ; i< 0xffffffff;i++){ }
NSLog(@"BusgDone");
} -(void)timerDone
{
NSLog(@"Timer Run");
}
执行结果为:
可以看到,timer本来都是以每隔1秒执行,毫秒都是.564,然后在进行复杂的运算时候,timer直接被delay了,当执行完BusyDone之后,立即执行了TimerRun,然后又在.564执行了TimerRun,而不是距离上次执行时间的1秒。
仅供参考。
iOS关于RunLoop和Timer的更多相关文章
- iOS多线程-RunLoop简介
什么是RunLoop? 从字面上来看是运行循环的意思. 内部就是一个do{}while循环,在这个循环里内部不断的处理各种任务(比如:source/timer/Observer) RunLoop的存在 ...
- ios之runloop笔记
网上关于runloop的文章不计其数,再此,贴个自认为讲的比较简单明了的文章 http://www.jianshu.com/p/536184bfd163 个人理解: ios的runloop应该是类似于 ...
- iOS开发RunLoop学习:四:RunLoop的应用和RunLoop的面试题
一:RunLoop的应用 #import "ViewController.h" @interface ViewController () /** 注释 */ @property ( ...
- iOS - OC RunLoop 运行循环/消息循环
1.RunLoop 1)运行循环: 运行循环在 iOS 开发中几乎不用,但是概念的理解却非常重要. 同一个方法中的代码一般都在同一个运行循环中执行,运行循环监听 UI 界面的修改事件,待本次运行循环结 ...
- iOS开发RunLoop
最近处于离职状态,时间也多了起来,但是学习还是不能放松,今天总结一下RunLoop,RunLoop属于iOS系统层的东西,还是比较重要的. 一.什么是RunLoop 字面意思看是跑圈,也可以看作运行循 ...
- iOS之RunLoop
RunLoop是iOS线程相关的比较重要的一个概念,无论是主线程还是子线程,都对应一个RunLoop,如果没有RunLoop,线程会马上被系统回收. 本文主要CFRunLoop的源码解析,并简单阐述一 ...
- iOS开发-Runloop详解(简书)
不知道大家有没有想过这个问题,一个应用开始运行以后放在那里,如果不对它进行任何操作,这个应用就像静止了一样,不会自发的有任何动作发生,但是如果我们点击界面上的一个按钮,这个时候就会有对应的按钮响应事件 ...
- iOS开发RunLoop学习:一:RunLoop简单介绍
一:RunLoop的简单介绍 #import "ViewController.h" @interface ViewController () @end @implementatio ...
- iOS开发 - RunLoop理解
RunLoop概念 运行循环,一个 run loop 就是一个事件处理的循环,用来不停的调度工作以及处理事件 作用 保持程序的持续运行 监听处理App中的各种事件(触摸事件,定时器事件,selecto ...
随机推荐
- Android dumpsys命令的使用
Android提供的dumpsys工具能够用于查看手机中的应用程序和系统服务信息与状态,手机连接电脑后能够直接命令行运行adb shell dumpsys 查看全部支持的Service可是这样输出的太 ...
- 今天才知道css hack是什么
先来个冷笑话:一晚下班回家,一民警迎面巡逻而来.突然对我大喊:站住! 民警:int类型占几个字节? 我:4个. 民警:你可以走了. 我感到很诧异. 我:为什么问这样的问题? 民警:深夜还在街上走,寒酸 ...
- c#数据库操作大全
原文:c#数据库操作大全 1.提取单条记录 //using System.Data; //using System.Data.SqlClient; using (SqlConnection cn = ...
- STL慎重选择删除元素的
一.要删除容器中有特定值的全部对象 1.假设容器是vector.string或deque.则使用erase-remove习惯使用方法.比如: vector<int> c; c.era ...
- visual studio 辅助工具
resharper 这是一个收费软件 需要下载对应版本的注册机. 效果如图所示: 这里的using 很多没有用到,他会用灰色标记,你都可以统统去掉. 声明一个类 ,要求 首字母大写,如果你小写了,他 ...
- .net mvc mssql easyui treegrid 及时 编辑 ,支持拖拽
这里提到了,1个问题,怎么扩展 Easyui 参见: http://blog.csdn.net/chenkai6529/article/details/17528833 @{ ViewBag.Titl ...
- 给Notepad++ 加右键菜单带图标
原文:给Notepad++ 加右键菜单带图标 从网上下载下来的Notepad++ http://download.tuxfamily.org/notepadplus/6.3.3/npp.6.3.3. ...
- App根据第,HTML5流行?
App根据第.HTML5流行? 引用新闻 日前,有消息称国家网信办近日将出台APP应用程序发展管理办法.中央网信办主任鲁炜在推进网络空间法治化座谈会上透露.我国将加强互联网立法,依靠严密的法律网来打造 ...
- WCF与Web API 区别
WCF与Web API 区别(应用场景) Web api 主要功能: 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, ...
- uva 11529 - Strange Tax Calculation(计数问题)
题目链接:uva 11529 - Strange Tax Calculation 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...