美文网首页iOS学习专题
dispatch_source_t 比 NSTimer 更准的定

dispatch_source_t 比 NSTimer 更准的定

作者: YM_1 | 来源:发表于2016-04-12 18:55 被阅读11441次

NSTimer受runloop的影响,由于runloop需要处理很多任务,导致NSTimer的精度降低,在日常开发中,如果我们需要对定时器的精度要求很高的话,可以考虑dispatch_source_t去实现 。dispatch_source_t精度很高,系统自动触发,系统级别的源。下面是通过dispatch_source_t 创建 计时器的例子

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    //开始时间
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);
    
    //间隔时间
    uint64_t interval = 2.0 * NSEC_PER_SEC;
    
    
    dispatch_source_set_timer(self.timer, start, interval, 0);
    
    //设置回调
    dispatch_source_set_event_handler(self.timer, ^{
    
        NSLog(@"----self.timer---");
    });
    
    
    //启动timer
    dispatch_resume(self.timer);

相关文章

  • dispatch_source_t 比 NSTimer 更准的定

    NSTimer受runloop的影响,由于runloop需要处理很多任务,导致NSTimer的精度降低,在日常开发...

  • dispatch_source_t 比 NSTimer 更准的定

    NSTimer受runloop的影响,由于runloop需要处理很多任务,导致NSTimer的精度降低,在日常开发...

  • 关于dispatch_source_t创建计时器

    dispatch_source_t 的优点: 1、dispatch_source_t 不再需要和NSTimer一样...

  • GCD 实现定时器

    NSTimer的定时器方法 About NSTimer GCD中使用dispatch_source_t实现定时器 ...

  • iOS 三种定时器切换

    先过好自己,才能过好人生。 CADisplayLink, NSTimer,dispatch_source_t 三种...

  • iOS 定时器

    iOS常用定时器有3种 NSTimer GCD定时器 dispatch_source_t CADisplayLin...

  • GCD定时器

    NSTimer 定时器易受 RunLoop模式影响导致定时器不准确。 dispatch_source_t time...

  • 2018-02-01

    iOS【NSTimer到底准不准?】 2018.2.1 最近面试 被 问道 NSTimer 到底准不准的问题 当时...

  • GCD timer

    1.GCD提供了一个类似于NSTimer的类:dispatch_source_t 这个类的特点: GCD的time...

  • 定时器

    NSTimer 1.方法介绍2.正确的使用NSTimer3.NSTimer的本质4.NSTimer准吗?5.per...

网友评论

    本文标题:dispatch_source_t 比 NSTimer 更准的定

    本文链接:https://www.haomeiwen.com/subject/txrwlttx.html