【iOS开发每日小笔记(九)】在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法

这篇文章是我的【iOS开发每日小笔记】系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧。它们可能会给用户体验、代码效率得到一些提升,或是之前自己没有接触过的技术,很开心的学到了,放在这里得瑟一下。90%的作用是帮助自己回顾、记忆、复习。

 

一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子。正巧自己的上线App Store的小游戏《跑酷好基友》(https://itunes.apple.com/us/app/pao-ku-hao-ji-you/id914554369?mt=8中有一个很好的实际使用例子。游戏中有一个计时功能。在1.0版本中,使用了简单的在主线程中调用:

1 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

的方法。但是当每0.01秒进行一次repeat操作时,NSTimer是不准的,严重滞后,而改成0.1秒repeat操作,则这种滞后要好一些。

导致误差的原因是我在使用“scheduledTimerWithTimeInterval”方法时,NSTimer实例是被加到当前runloop中的,模式是NSDefaultRunLoopMode。而“当前runloop”就是应用程序的main runloop,此main runloop负责了所有的主线程事件,这其中包括了UI界面的各种事件。当主线程中进行复杂的运算,或者进行UI界面操作时,由于在main runloop中NSTimer是同步交付的被“阻塞”,而模式也有可能会改变。因此,就会导致NSTimer计时出现延误。

解决这种误差的方法,一种是在子线程中进行NSTimer的操作,再在主线程中修改UI界面显示操作结果;另一种是仍然在主线程中进行NSTimer操作,但是将NSTimer实例加到main runloop的特定mode(模式)中。避免被复杂运算操作或者UI界面刷新所干扰。

方法一:

在开始计时的地方:

1 if (self.timer) {
2         [self.timer invalidate];
3         self.timer = nil;
4     }
5     self.timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(addTime) userInfo:nil repeats:YES];
6     [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

[NSRunLoop currentRunLoop]获取的就是“main runloop”,使用NSRunLoopCommonModes模式,将NSTimer加入其中。

(借鉴了博文:http://www.mgenware.com/blog/?p=459

 

方法二:

开辟子线程:(使用子线程的runloop)

1 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread) object:nil];
2     [thread start];
1 - (void)newThread
2 {
3     @autoreleasepool
4     {
5         [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(addTime) userInfo:nil repeats:YES];
6         [[NSRunLoop currentRunLoop] run];
7     }
8 }

在子线程中将NSTimer以默认方式加到该线程的runloop中,启动子线程。

 

方法三:

使用GCD,同样也是多线程方式:

声明全局成员变量

1 dispatch_source_t _timers;
 1     uint64_t interval = 0.01 * NSEC_PER_SEC;
 2     dispatch_queue_t queue = dispatch_queue_create("my queue", 0);
 3     _timers = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
 4     dispatch_source_set_timer(_timers, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);
 5     __weak ViewController *blockSelf = self;
 6     dispatch_source_set_event_handler(_timers, ^()
 7     {
 8         NSLog(@"Timer %@", [NSThread currentThread]);
 9         [blockSelf addTime];
10     });
11     dispatch_resume(_timers);

然后在主线程中修改UI界面:

1 dispatch_async(dispatch_get_main_queue(), ^{
2         self.label.text = [NSString stringWithFormat:@"%.2f", self.timeCount/100];
3     });

游戏源代码可见:https://github.com/pigpigdaddy/BothLive

 

总结:

runloop是一个看似很神秘的东西,其实一点也不神秘。每个线程都有一个实际已经存在的runloop。比如我们的主线程,在主函数的UIApplication中:

1 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]))

系统就为我们将主线程的main runloop隐式的启动了。runloop顾名思义就是一个“循环”,他不停地运行,从程序开始到程序退出。正是由于这个“循环”在不断地监听各种事件,程序才有能力检测到用户的各种触摸交互、网络返回的数据才会被检测到、定时器才会在预定的时间触发操作……

runloop只接受两种任务:输入源和定时源。本文中说的就是定时源。默认状态下,子线程的runloop中没有加入我们自己的源,那么我们在子线程中使用自己的定时器时,就需要自己加到runloop中,并启动该子线程的runloop,这样才能正确的运行定时器。

 

参考文章:

http://www.mgenware.com/blog/?p=459

http://blog.csdn.net/wzzvictory/article/details/9237973

http://www.dahuangphone.com/dv_rss.asp?s=xhtml&boardid=8&id=93&page=5

http://www.hrchen.com/2013/07/tricky-runloop-on-ios/

http://www.cnblogs.com/vicstudio/p/3281682.html

posted @ 2014-09-16 22:38  pigpigdaddy  阅读(4177)  评论(2编辑  收藏  举报