GCD

总是习惯把知识记录在本地,是时候改变一下存储知识的方式了,丰富自己,帮助他人!

1.GCD 的一般用法

     dispatch_async(dispatch_get_global_queue(00), ^{

   //耗时操作。。

      

        dispatch_async(dispatch_get_main_queue(), ^{        

   //通知主线程刷新

        })

});

 

 

2.用GCD做定时器 

@property (nonatomic) dispatch_source_t timerSource;

- (void)updateWithInte:(NSInteger) time{

    NSTimeInterval period = time; //设置时间间隔

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    _timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

    dispatch_source_set_timer(_timerSource, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行

    dispatch_source_set_event_handler(_timerSource, ^{

        NSLog(@"nihao");

    });

    dispatch_resume(_timerSource);

}

作者:啡色的灰机------------------------------------------------------------------------------------------------------------ 邮箱:developer_yh@163.com--------------------------------------------------------------------------------------------- GitHub:https://github.com/developeryh
原文地址:https://www.cnblogs.com/devyh/p/5313506.html