zoukankan      html  css  js  c++  java
  • 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法

    一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子。游戏中有一个计时功能在主线程中调用:

    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加入其中。

    (借鉴了博文:)

     

    方法二:

    开辟子线程:(使用子线程的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     });

    游戏源代码可见:

    总结:

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

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

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

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

  • 相关阅读:
    Python基础---------数据类型
    Linux----------Rabbitmq消息队列
    常用消息中间件对比
    Linux-------------kafaka基础
    Linux------------zookeeper基础
    CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
    Linux-------------MongoDB基础
    Web Service
    AutoResetEvent和ManualResetEvent(多线程操作)
    方法的可变参数 params
  • 原文地址:https://www.cnblogs.com/AlvinCrash/p/5379517.html
Copyright © 2011-2022 走看看