一、函数的任务
1、创建和管理队列
dispatch_get_global_queue
dispatch_get_main_queue
dispatch_queue_create
dispatch_get_current_queue
dispatch_queue_get_label
dispatch_set_target_queue
dispatch_main
2、队列任务分发
dispatch_async
dispatch_async_f
dispatch_sync
dispatch_sync_f
dispatch_after
dispatch_after_f
dispatch_apply
dispatch_apply_f
dispatch_once
3、使用分发组
dispatch_group_async
dispatch_group_async_f
dispatch_group_create
dispatch_group_enter
dispatch_group_leave
dispatch_group_notify
dispatch_group_notify_f
dispatch_group_wait
4、管理分发对象
dispatch_debug
dispatch_get_context
dispatch_release
dispatch_resume
dispatch_retain
dispatch_set_context
dispatch_set_finalizer_f
dispatch_suspend
5、Using Semaphores
dispatch_semaphore_create
dispatch_semaphore_signal
dispatch_semaphore_wait
6、Using Barriers
dispatch_barrier_async
dispatch_barrier_async_f
dispatch_barrier_sync
dispatch_barrier_sync_f
7、管理分发来源
dispatch_source_cancel
dispatch_source_create
dispatch_source_get_data
dispatch_source_get_handle
dispatch_source_get_mask
dispatch_source_merge_data
dispatch_source_set_registration_handler
dispatch_source_set_registration_handler_f
dispatch_source_set_cancel_handler
dispatch_source_set_cancel_handler_f
dispatch_source_set_event_handler
dispatch_source_set_event_handler_f
dispatch_source_set_timer
dispatch_source_testcancel
8、使用分发I / O便利的API
dispatch_read
dispatch_write
9、使用分发I / O通道API
dispatch_io_create
dispatch_io_create_with_path
dispatch_io_read
dispatch_io_write
dispatch_io_close
dispatch_io_set_high_water
dispatch_io_set_low_water
dispatch_io_set_interval
10、管理分发数据对象
dispatch_data_create
dispatch_data_get_size
dispatch_data_create_map
dispatch_data_create_concat
dispatch_data_create_subrange
dispatch_data_apply
dispatch_data_copy_region
11、管理时间
dispatch_time
dispatch_walltime
12、Managing Queue-Specific Context Data
dispatch_queue_set_specific
dispatch_queue_get_specific
dispatch_get_specific
二、函数
1、dispatch_after
//延迟2.0s执行
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//do something...
});
2、dispatch_apply
// 对于同步执行,这个函数调用单一block多次,并平行运算,然后等待所有运算结束。和数组循环有点类似
dispatch_queue_t queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply([array count], queue3, ^(size_t index){
[self doSomethingIntensiveWith:[array objectAtIndex:index]];
});
// 全局队列中后台执行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
4、dispatch_barrier_async
// dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行
dispatch_queue_t queue6 = dispatch_queue_create("gcdtest.zfl.demo", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue6, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"dispatch_async1");
});
dispatch_async(queue6, ^{
NSLog(@"dispatch_async2");
});
dispatch_barrier_async(queue6, ^{
NSLog(@"dispatch_barrier_async");
});
dispatch_async(queue6, ^{
NSLog(@"dispatch_async3");
});
5、dispatch_data_apply
//here is a function which transforms a data object containing ASCII data into an NSString using dispatch_data_apply to avoid unnecessary copies:
NSString *StringFromDispatchData(dispatch_data_t data)
{
NSMutableString *s = [NSMutableString stringWithCapacity: dispatch_data_get_size(data)];
dispatch_data_apply(data, ^(dispatch_data_t region, size_t offset, const void *buffer, size_t size) {
[s appendFormat: @"%.*s", size, buffer];
return (_Bool)true;
});
return s;
}
6、dispatch_get_global_queue
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //获取全局队列
7、dispatch_get_main_queue
dispatch_get_main_queue(); //获取主队列
8、dispatch_queue_create
dispatch_queue_t queue = dispatch_queue_create("blog.devtang.com", NULL); //创建一个队列
9、dispatch_group_wait
// 一个dispatch group可以用来将多个block组成一组以监测这些Block全部完成或者等待全部完成时发出的消息。使用函数dispatch_group_create来创建,然后使用函数dispatch_group_async来将block提交至一个dispatch queue,同时将它们添加至一个组。
dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group1 = dispatch_group_create();
NSArray *array = [NSArray array];
for(id obj in array)
dispatch_group_async(group1, queue1, ^{
[self doSomethingIntensiveWith:obj];
});
dispatch_group_wait(group1, DISPATCH_TIME_FOREVER);
dispatch_release(group1);