概覽
- 創(chuàng)建一個(gè)調(diào)度源指定源類型答憔、回調(diào)一汽、綁定mask、設(shè)置隊(duì)列
dispatch_source_t
dispatch_source_create(dispatch_source_type_t type,
uintptr_t handle,
uintptr_t mask,
dispatch_queue_t _Nullable queue);
- 為調(diào)度源source設(shè)置回調(diào)block
void
dispatch_source_set_event_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 為調(diào)度源source設(shè)置回調(diào)c函數(shù)指針
void
dispatch_source_set_event_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
- 為調(diào)度源source設(shè)置取消時(shí)調(diào)用的回調(diào)block
dispatch_source_set_cancel_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 為調(diào)度源source設(shè)置取消時(shí)調(diào)用的回調(diào)c函數(shù)指針
void
dispatch_source_set_cancel_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
- 異步取消調(diào)度源
void
dispatch_source_cancel(dispatch_source_t source);
- 檢測(cè)調(diào)度源是否被取消
intptr_t
dispatch_source_testcancel(dispatch_source_t source);
- 獲取調(diào)度源綁定的回調(diào)
uintptr_t
dispatch_source_get_handle(dispatch_source_t source);
獲取調(diào)度源綁定的mask
uintptr_t
dispatch_source_get_mask(dispatch_source_t source);
獲取調(diào)度源的待處理數(shù)據(jù)
uintptr_t
dispatch_source_get_data(dispatch_source_t source);
- 將數(shù)據(jù)合并到調(diào)度源中,并將回調(diào)提交到目標(biāo)隊(duì)列
void
dispatch_source_merge_data(dispatch_source_t source, uintptr_t value);
- 設(shè)置計(jì)時(shí)器源的開始時(shí)間、時(shí)間間隔瓢娜、回程值
void
dispatch_source_set_timer(dispatch_source_t source,
dispatch_time_t start,
uint64_t interval,
uint64_t leeway);
- 為調(diào)度源設(shè)置激活后就調(diào)用的回調(diào)block
void
dispatch_source_set_registration_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler);
- 為調(diào)度源設(shè)置激活后就調(diào)用的回調(diào)c函數(shù)指針
void
dispatch_source_set_registration_handler_f(dispatch_source_t source,
dispatch_function_t _Nullable handler);
使用場(chǎng)景
累加
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}
使用場(chǎng)景:
所有合并操作執(zhí)行完畢會(huì)執(zhí)行dispatch_source_set_event_handler,就是一組data合并之后執(zhí)行一個(gè)回調(diào)
定時(shí)器
+ (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);
}
}