美文网首页
GCD定时器

GCD定时器

作者: 妖妖零幺幺 | 来源:发表于2018-06-29 16:44 被阅读0次

GCD定时器会自己开启一条子线程,子线程也会自己开启runloop,自己创建管理,所以需要高精度计时时,用GCD计时器。一般情况NSTimer即可。

   // 创建定时器对象 1、source类型 4、在哪个线程中执行
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    // 设置定时器 1、定时器对象 2、开始时间 3、间隔时间,单位:纳秒 4、允许误差
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 10.0 * NSEC_PER_SEC, 0);
    // 定时执行的任务 
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"GCD timer test");
    });
    // 恢复(默认是停止的)
    dispatch_resume(timer);

相关文章

网友评论

      本文标题:GCD定时器

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