参考资料:
YYTimer
是基于GCD的线程安全的计时器,他有和NSTimer
类似的API,YYTimer
对象和NSTimer
在下面几个方式不同:
- 它使用GCD产生时钟tick,不受runloop影响。
- 它对target是weak引用,所以可以避免引用循环。
- 它在主线程上开启。
//1.创建类型为 定时器类型的 Dispatch Source
//1.1将定时器设置在主线程
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
//1.2设置定时器每一秒执行一次
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1ull * NSEC_PER_SEC, 0);
//1.3设置定时器执行的动作
dispatch_source_set_event_handler(_timer, ^{
//在这里实现业务逻辑...
NSLog(@“业务开始了!");
});
//2.启动定时器
dispatch_resume(_timer);
注意:将定时器写成属性,是因为内存管理的原因,使用了dispatch_source_create
方法,这种方法GCD是不会帮你管理内存的。dispatch_source_create(dispatch_source_type_t type,
uintptr_t handle,
unsigned long mask,
dispatch_queue_t _Nullable queue)
第一个参数:dispatch_source_type_t type
为设置GCD源方法的类型
第二个参数:uintptr_t handle
暂时没有使用,传0即可。
第三个参数:unsigned long mask
使用DISPATCH_TIMER_STRICT
,会引起电量消耗加剧,毕竟要求精确时间,所以一般传0即可,视业务情况而定。
第四个参数:dispatch_queue_t _Nullable queue
队列,将定时器事件处理的Block提交到哪个队列之上。可以传Null,默认为全局队列。注意:当提交到全局队列的时候,时间处理的回调内,需要切换到主线程,更新UI
dispatch_source_set_timer(dispatch_source_t source,
dispatch_time_t start,
uint64_t interval,
uint64_t leeway);
第一个参数:dispatch_source_t source
第二个参数:dispatch_time_t start
, 定时器开始时间,类型为 dispatch_time_t
,其API的abstract标明可参照dispatch_time()
和dispatch_walltime()
,同为设置时间,但是后者为“钟表”时间,相对比较准确,所以选择使用后者。dispatch_walltime(const struct timespec *_Nullable when, int64_t delta)
,参数when可以为Null,默认为获取当前时间,参数delta为增量,即获取当前时间的基础上,增加X秒的时间为开始计时时间,此处传0即可。
第三个参数:uint64_t interval
,定时器间隔时长,由业务需求而定。
第四个参数:uint64_t leeway
, 允许误差,此处传0即可。
dispatch_source_set_event_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler)
第一个参数:dispatch_source_t source
第二个参数:dispatch_block_t _Nullable handler
,定时器执行的动作,需要处理的业务逻辑Block。
dispatch_resume(_timer)
定时器创建完成并不会运行,需要主动去触发,也就是调用上述方法。
调度源提供了源事件的处理回调,同时也提供了取消源事件处理的回调,使用非常方便。
dispatch_source_set_cancel_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler)