关于NSTimer的一些注意事项

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

解决这种误差的方法,一种是在子线程中进行NSTimer的操作,再在主线程中修改UI界面显示操作结果;另一种是仍然在主线程中进行NSTimer操作,但是将NSTimer实例加到main runloop的特定mode(模式)中。避免被复杂运算操作或者UI界面刷新所干扰。
(个人感觉如果需要做到界面刷新操作时候NSTimer不收影响又不阻塞线程的情况下,将NSTimer放到子线程是更好的解决方法).

方法一:

在开始计时的地方:

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

方法二:

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

 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread) object:nil];
    [thread start];

- (void)newThread
 {
    @autoreleasepool
    {
         [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(addTime) userInfo:nil repeats:YES];
         [[NSRunLoop currentRunLoop] run];
     }
 }

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

方法三:

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

声明全局成员变量

    dispatch_source_t _timers;


    uint64_t interval = 0.01 * NSEC_PER_SEC;
    dispatch_queue_t queue = dispatch_queue_create("my queue", 0);
    _timers = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timers, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);
    __weak ViewController *blockSelf = self;
     dispatch_source_set_event_handler(_timers, ^()
   {
       NSLog(@"Timer %@", [NSThread currentThread]);
         [blockSelf addTime];
    });
    dispatch_resume(_timers);

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

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

总结:

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

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

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

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

摘录自:http://www.cnblogs.com/pigpigDD/p/3975852.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值