概览
- 创建一个调度源指定源类型、回调、绑定mask、设置队列
dispatch_source_t
dispatch_source_create(dispatch_source_type_t type,
uintptr_t handle,
uintptr_t mask,
dispatch_queue_t _Nullable queue);
- 为调度源source设置回调block
void
dispatch_source_set_event_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 为调度源source设置回调c函数指针
void
dispatch_source_set_event_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
- 为调度源source设置取消时调用的回调block
dispatch_source_set_cancel_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 为调度源source设置取消时调用的回调c函数指针
void
dispatch_source_set_cancel_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
- 异步取消调度源
void
dispatch_source_cancel(dispatch_source_t source);
- 检测调度源是否被取消
intptr_t
dispatch_source_testcancel(dispatch_source_t source);
- 获取调度源绑定的回调
uintptr_t
dispatch_source_get_handle(dispatch_source_t source);
获取调度源绑定的mask
uintptr_t
dispatch_source_get_mask(dispatch_source_t source);
获取调度源的待处理数据
uintptr_t
dispatch_source_get_data(dispatch_source_t source);
- 将数据合并到调度源中,并将回调提交到目标队列
void
dispatch_source_merge_data(dispatch_source_t source, uintptr_t value);
- 设置计时器源的开始时间、时间间隔、回程值
void
dispatch_source_set_timer(dispatch_source_t source,
dispatch_time_t start,
uint64_t interval,
uint64_t leeway);
- 为调度源设置激活后就调用的回调block
void
dispatch_source_set_registration_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 为调度源设置激活后就调用的回调c函数指针
void
dispatch_source_set_registration_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
使用场景
累加
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(source,^{
uintptr_t data = dispatch_source_get_data(source);
NSLog(@"data = %lu, 线程:%@", data, [NSThread currentThread]);
});
dispatch_resume(source);
NSInteger count = 6;
while (count--) {
dispatch_source_merge_data(source, count);
NSLog(@"data + %ld", (long)count);
}
输出:
2021-08-22 16:02:16.857397+0800 GCDDemo[4081:758127] data + 5
2021-08-22 16:02:16.857470+0800 GCDDemo[4081:758127] data + 4
2021-08-22 16:02:16.857499+0800 GCDDemo[4081:758127] data + 3
2021-08-22 16:02:16.857526+0800 GCDDemo[4081:758127] data + 2
2021-08-22 16:02:16.857550+0800 GCDDemo[4081:758127] data + 1
2021-08-22 16:02:16.857574+0800 GCDDemo[4081:758127] data + 0
2021-08-22 16:02:16.865296+0800 GCDDemo[4081:758127] data = 15, 线程:<NSThread: 0x2815a8980>{number = 1, name = main}
使用场景:
所有合并操作执行完毕会执行dispatch_source_set_event_handler,就是一组data合并之后执行一个回调
定时器
+ (dispatch_source_t)gcdTimerRepeatInterval:(Float64)interval
afterSeconds:(Float64)seconds
identifier:(const char *)identifier
callback:(dispatch_block_t)block
cancelBlock:(dispatch_block_t)cancelBlock {
dispatch_queue_t queue = dispatch_queue_create(identifier, DISPATCH_QUEUE_CONCURRENT);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), interval * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
if (block) {
block();
}
});
dispatch_source_set_cancel_handler(timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
if (cancelBlock) {
cancelBlock();
}
});
});
return timer;
}
+ (void)gcdTimerStart:(dispatch_source_t)timer {
if (timer) {
dispatch_resume(timer);
}
}
+ (void)gcdTimerStop:(dispatch_source_t)timer {
if (timer) {
dispatch_source_cancel(timer);
}
}
网友评论