美文网首页
iOS GCD封装Timer

iOS GCD封装Timer

作者: KOLee | 来源:发表于2020-04-12 17:59 被阅读0次

欢迎加QQ群: 457236811 ,我们一起来搞基!

一、为啥要用GCD封装Timer

  • 为什么要用GCD封装Timer呢?主要是系统提供的NSTimer可能不准,有同学就问为啥苹果提供的Timer为啥就不准了吖?这么麻烦要自己造

  • 其实啊 主要还是NSTimer是基于NSRunloop封装的,这个鬼东西是啥?感兴趣的同学可以去查查,我这里就把大概说一下

1.NSRunloop有两种Mode 分别为NSDefaultRunLoopMode、NSRunLoopCommonModes当她切换currentMode的时候,Timer会失效(就是不会跑了)
2.NSRunloop她相当于一个循环,她很忙的时候要一圈,一圈的跑,这样她就跑到不均匀,绕圈圈的时间不一样,而且跑每圈的固定位置才处理Timer,所以NSTimer可能因为NSRunloop太忙就不准(主要原因)
3.那么GCD封装的好处呢? GCD基于系统内核管理,不依赖于NSRunloop,而且听说能精确到纳秒级别

二、用GCD封装Timer

  • 说了那么多终于可以开干了(怎么这么啰嗦),直接上代码吧:
/// 初始化timer 返回timer唯一标识符
/// @param task 回调block
/// @param start 多少秒后开始
/// @param interval 定时器间隔时间
/// @param repeats 是否重复
/// @param async 是否在异步线程创建timer
+(NSString *)execTask:(void(^)(void))task
                start:(NSTimeInterval)start
             interval:(NSTimeInterval)interval
              repeats:(BOOL)repeats
                async:(BOOL)async;


/// 取消对应的timr
/// @param name timer唯一标识符
+ (void)cancelTask:(NSString *)name;
  • .m文件代码实现:
//存放定时器的全局字典
static NSMutableDictionary *timers;
// 信号量加锁
dispatch_semaphore_t semaphore;

+ (void)initialize {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        semaphore = dispatch_semaphore_create(1);
        timers = [NSMutableDictionary dictionary];
    });
}

+ (NSString *)execTask:(void (^)(void))task
                 start:(NSTimeInterval)start
              interval:(NSTimeInterval)interval
               repeats:(BOOL)repeats
                 async:(BOOL)async
{
    
    if (!task || interval<=0 || start<0) return nil;
    
    static int i = 0;
    
    // 定时器唯一标识符
    NSString *identify = [NSString stringWithFormat:@"%d",i++];
        
    // 队列
    dispatch_queue_t queue = async ? dispatch_get_global_queue(0, 0) : dispatch_get_main_queue();
    
    // 创建定时器
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    // 设置时间
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, start * NSEC_PER_SEC), interval * NSEC_PER_SEC, 0);
    
    // 设置回调
    dispatch_source_set_event_handler(timer,^{
        
        task();
        if (!repeats) {
            [self cancelTask:identify];
        }
    });
    
    // 启动定时器
    dispatch_resume(timer);
    
    // 加锁
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    // 存放到全局字典
    timers[identify] = timer;
    // 解锁
    dispatch_semaphore_signal(semaphore);

    return identify;
}

+ (void)cancelTask:(NSString *)name
{
    if (name.length > 0) {
        // 加锁
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_source_cancel(timers[name]);
        // 移除对应的timer
        [timers removeObjectForKey:name];
        // 解锁
        dispatch_semaphore_signal(semaphore);
    }

}
  • 最后里面有注释,但是有些东西还是要讲一讲的,这样才好嘛

1.为什么用全局字典存放timer?方便取出来取消Timer(AFN SDWebImage 也是这么干)
2.为什么用dispatch_semaphore加锁,NSMutableDictionary多线程下不安全 而且dispatch_semaphore加锁速度快 (那个YYKit 大神有个测评,自旋锁不安全,互斥锁这个最快的)嗯 就是这样

相关文章

网友评论

      本文标题:iOS GCD封装Timer

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