GCD定时器

GCD定时器要比NSTimer时间准确,为什么?因为NSTimer是受RunLoop模式影响的,但如果设置Track模式,也好使!而GCD是不受RunLoop模式影响的。面试官经常会问什么定时器不准确,怎么办?

这个不是定时器,这只是延迟调用,执行完后就不做事情了

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"这个不是定时器");
    });

定时器,这里有几个注意点:

  • dispatch_source_t 是一个OC对象,所有要强引用
  • 虽然是create创建,但这里不用release
  • GCD的时间参数是纳秒,所有时间特别准
  • GCD定时器默认是关闭的
#import "ViewController.h"

@interface ViewController ()

/** 定时器对象(不用带*,因为dispatch_source_t是个类,内部已经包含了*) */
@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation ViewController

int count = 0;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    // 创建一个定时器(dispatch_source_t本质是个oc对象)
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    // 设置定时器的属性(几时开始,每隔多久执行任务)
    // GCD的时间参数是纳秒(一秒等于十的九次方纳秒)
    // dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC)比当前时间晚3秒
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC));
    uint64_t end = (uint64_t)(2.0 * NSEC_PER_SEC);
    dispatch_source_set_timer(self.timer, start, end, 0);
    
    // 设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        NSLog(@"--------%@", [NSThread currentThread]);
        count ++;
        if (count == 10) {
            // 取消定时器
            dispatch_cancel(self.timer);
            self.timer = nil;
        }
        
    });
    
    // 开启定时器
    dispatch_resume(self.timer);
    
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 提到定时器,NStimer肯定是我们最为熟悉的。 但是NStimer有着很大的缺点,并不准确。 通俗点说,就是它该...
    mengyingguo阅读 382评论 0 0
  • 大家平时开发中使用最多的定时器应该是NSTimer了,但是,NSTimer同时也存在一些弊端:比如,有时候你要把它...
    忧郁的小码仔阅读 1,022评论 0 4
  • NSTimer 定时器易受 RunLoop模式影响导致定时器不准确。 dispatch_source_t time...
    ccme阅读 170评论 0 0
  • GCD定时器不受RunLoop影响,比NSTimer更精确 //获得队列 //dispatch_queue_t q...
    rlqs阅读 425评论 0 0
  • 上个月,上财政管理课课间休息的时候,老师突然间放了一首陈小春的独家记忆, 有关于你,绝口不提,没关系 喜欢你,是我...
    冬天的木头阅读 293评论 0 1