计时器dispatch_source_set_timer

/**
 创建定时器, 时间单位都是秒

 @param interval 回调间隔
 @param leeway 精度
 @param queue 定时器调用队列
 @param block 任务快
 @return 返回的定时器对象
 */
NS_INLINE dispatch_source_t TBCreateDispatchTimer(uint64_t interval,
                                                  uint64_t leeway,
                                                  dispatch_queue_t queue,
                                                  dispatch_block_t block)
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    if (timer)
    {
        dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), interval * NSEC_PER_SEC, leeway * NSEC_PER_SEC);
        dispatch_source_set_event_handler(timer, block);
        dispatch_resume(timer);
    }
    return timer;
}

NS_INLINE dispatch_source_t TBCreateDispatchTimerV2(uint64_t interval,
                                                  uint64_t leeway,
                                                  uint64_t beginInterval,
                                                  dispatch_queue_t queue,
                                                  dispatch_block_t block)
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    if (timer)
    {
        dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, beginInterval * NSEC_PER_SEC), interval * NSEC_PER_SEC, leeway * NSEC_PER_SEC);
        dispatch_source_set_event_handler(timer, block);
        dispatch_resume(timer);
    }
    return timer;
}

/// 创建timer
/// @param interval 间隔,单位秒,double类型
/// @param leeway   精度,单位秒,double类型
/// @param beginInterval 开始时间,单位秒,double类型
/// @param queue 队列
/// @param block timer执行的block
NS_INLINE dispatch_source_t TBCreateDispatchTimerV3(double interval,
                                                  double leeway,
                                                  double beginInterval,
                                                  dispatch_queue_t queue,
                                                  dispatch_block_t block)
{
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    if (timer)
    {
        dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW,
                                                       (uint64_t)(beginInterval * NSEC_PER_SEC)),
                                  (uint64_t)(interval * NSEC_PER_SEC),
                                  (uint64_t)(leeway * NSEC_PER_SEC));
        dispatch_source_set_event_handler(timer, block);
        dispatch_resume(timer);
    }
    return timer;
}
  

你可能感兴趣的:(计时器dispatch_source_set_timer)