NSTimer与NSRunLoop的关系分析
2015-09-18 17:12
253 查看
最近关于NSTimer和NSRunLoop的关系,做了一个小试验。代码地址:https://github.com/TianLibin/timerRunLoopTest.git
代码运行效果如下图所示:
本示例演示了四个定时器的效果以及界面操作对它们的影响。
前两个定时器,是在子线程中启动的:
- (void)subThread1
{
@autoreleasepool {
self.subThreadTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(subThread1Fun:)
userInfo:nil
repeats:YES];
}
}
- (void)subThread2
{
@autoreleasepool {
self.subThreadTimer2 = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(subThread2Fun:)
userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run]; // 这行代码是两个定时器启动的不同之处
}
}
但是从运行效果看,第一个标签数字没有变化,一直是0,明显是定时器没有执行到。
两者的区别在于第二个多执行了一行代码:[[NSRunLoop currentRunLoop] run];
因为NSRunLoop在主线程中是默认运行的,子线程中默认不运行。所以在第一个定时器中,虽然设置了定时器,但出了该线程方法,runLoop默认停止了,异步的方法也就执行不到了。
第二个定时器,在线程中显示把runLoop运行起来了,它会一直运行下去,直到异步方法执行完毕。
下面比较第三,四两个定时器的效果:
- (void)startDefaultRunTimer
{
self.defaultRunTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(defaultRunFun:)
userInfo:nil
repeats:YES];
}
- (void)startCommonRunTimer
{
self.commonRunTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(commonRunFun:)
userInfo:nil
repeats:YES];
// 下面一行代码是这两个定时器启动的不同之处
[[NSRunLoop currentRunLoop] addTimer:self.commonRunTimer forMode:NSRunLoopCommonModes];
}
从开始运行,这两个定时器没有什么不同之处。但是当我把手指按在下面左侧的滚动条上上下拖动滚动视图时,这时为了更清楚一看到区别,手指不要离开屏幕。能看到第三个标签数字停止变化,即定时器方法暂停执行了,松开手指后,数字继续变化。
两个定时器的区别在于这句话:[[NSRunLoop currentRunLoop] addTimer:self.commonRunTimer forMode:NSRunLoopCommonModes];
因为runLoop默认的运行模式是:NSDefaultRunLoopMode。在iOS系统下,为了提高界面的响应速度,在用于对屏幕进行操作时,会暂停一些运算,尤其是涉及到滚动视图这种需要流畅响应的视图。
用手拖动右侧滚动试图没有影响,因为它不会上下滚动。
所以我们显示地把这个定时器加到NSRunLoopCommonModes运行模式下,就会忽略界面操作的影响,可以不受界面操作的干扰,正常执行异步方法了。
代码运行效果如下图所示:
本示例演示了四个定时器的效果以及界面操作对它们的影响。
前两个定时器,是在子线程中启动的:
- (void)subThread1
{
@autoreleasepool {
self.subThreadTimer1 = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(subThread1Fun:)
userInfo:nil
repeats:YES];
}
}
- (void)subThread2
{
@autoreleasepool {
self.subThreadTimer2 = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(subThread2Fun:)
userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] run]; // 这行代码是两个定时器启动的不同之处
}
}
但是从运行效果看,第一个标签数字没有变化,一直是0,明显是定时器没有执行到。
两者的区别在于第二个多执行了一行代码:[[NSRunLoop currentRunLoop] run];
因为NSRunLoop在主线程中是默认运行的,子线程中默认不运行。所以在第一个定时器中,虽然设置了定时器,但出了该线程方法,runLoop默认停止了,异步的方法也就执行不到了。
第二个定时器,在线程中显示把runLoop运行起来了,它会一直运行下去,直到异步方法执行完毕。
下面比较第三,四两个定时器的效果:
- (void)startDefaultRunTimer
{
self.defaultRunTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(defaultRunFun:)
userInfo:nil
repeats:YES];
}
- (void)startCommonRunTimer
{
self.commonRunTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(commonRunFun:)
userInfo:nil
repeats:YES];
// 下面一行代码是这两个定时器启动的不同之处
[[NSRunLoop currentRunLoop] addTimer:self.commonRunTimer forMode:NSRunLoopCommonModes];
}
从开始运行,这两个定时器没有什么不同之处。但是当我把手指按在下面左侧的滚动条上上下拖动滚动视图时,这时为了更清楚一看到区别,手指不要离开屏幕。能看到第三个标签数字停止变化,即定时器方法暂停执行了,松开手指后,数字继续变化。
两个定时器的区别在于这句话:[[NSRunLoop currentRunLoop] addTimer:self.commonRunTimer forMode:NSRunLoopCommonModes];
因为runLoop默认的运行模式是:NSDefaultRunLoopMode。在iOS系统下,为了提高界面的响应速度,在用于对屏幕进行操作时,会暂停一些运算,尤其是涉及到滚动视图这种需要流畅响应的视图。
用手拖动右侧滚动试图没有影响,因为它不会上下滚动。
所以我们显示地把这个定时器加到NSRunLoopCommonModes运行模式下,就会忽略界面操作的影响,可以不受界面操作的干扰,正常执行异步方法了。
相关文章推荐
- iptables 的防火墙正常运行时间和安全性 使用此高效应用程序设置并维护 Linux 防火墙
- (转)ecshop 后台商品分类添加图片的功能
- Hadoop 2.0中用户安全伪装/模仿机制实现原理
- 新浪、万网前系统架构师高俊峰:统一监控报警平台架构设计思路
- linux c 使用正则表达式
- Nginx+Keepalived搭建高可用负载均衡集群
- Linux永久关闭防火墙 vsftp 开机自启动
- PHPCMS搭建wap手机网站
- Linux chattr和lsattr
- MySQL学习5_CentOS下Mysql数据库(不使用yum命令)的安装与配置
- hadoop+maven工程伪分布下实例运行
- Shell 学习笔记
- linux中某个端口拒绝远程主机连接原因及解决方法
- linux问题-APR not Found
- window 与centos 系统之间共享文件
- 文件内容统计——Linux wc命令
- HDFS 架构设计
- linux问题-kali中安装apache和CGI
- 写作Openwrt固件
- CentOS下JDK安装