A。普通的GCD异步运行与主线程更新写法:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // long-running task dispatch_async(dispatch_get_main_queue(), ^{ // update UI }); });
DISPATCH_QUEUE_PRIORITY_DEFAULT 这个有3个参数,这里是使用默认的,其他两个参数分别是DISPATCH_QUEUE_PRIOPITY_HIGH 和 DISPATCH_QUEUE_PRIOPITY_LOW
B。任务串行的GCD
NSDate *da = [NSDate date]; NSString *daStr = [da description]; const char *queueName = [daStr UTF8String]; dispatch_queue_t myQueue = dispatch_queue_create(queueName, DISPATCH_QUEUE_SERIAL); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:6]; NSLog(@"[NSThread sleepForTimeInterval:6];"); }); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"[NSThread sleepForTimeInterval:3];"); }); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"[NSThread sleepForTimeInterval:1];"); }); dispatch_release(myQueue);
C。任务并行的GCD
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:6]; NSLog(@"[NSThread sleepForTimeInterval:6];"); }); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"[NSThread sleepForTimeInterval:3];"); }); dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"[NSThread sleepForTimeInterval:1];"); }); dispatch_release(myQueue);
D。实现监听一组任务是否完成,完成后得到通知执行其他的操作GCD
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:6]; NSLog(@"group1 [NSThread sleepForTimeInterval:6];"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"group2 [NSThread sleepForTimeInterval:3];"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"group3 [NSThread sleepForTimeInterval:1];"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"main thread."); }); dispatch_release(group);
E。前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行GCD
dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"dispatch_async1"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"dispatch_async2"); }); dispatch_barrier_async(queue, ^{ NSLog(@"dispatch_barrier_async"); [NSThread sleepForTimeInterval:0.5]; }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"dispatch_async3"); });
F。延时执行GCD
double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // code to be executed on the main queue after delay });
G。保证整个应用程序生命周期中某段代码只被执行一次GCD
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // code to be executed once });
H。执行某个代码片段N次GCD
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(5, queue, ^(size_t index) { // 执行5次 });
I。通过dispatch_queue_create函数创建的queue,其优先级的线程和采用默认优先级的Global dispatch queue的线程是相同的,那么如何改变通过dispatch_queue_create创建queue的优先级呢?可以通过dispatch_set_target_queue这个函数该实现。
dispatch_queue_t myQueue = dispatch_queue_create("org.itjoy.gcd", NULL); dispatch_queue_t globalHightQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_set_target_queue(myQueue, globalHighQueue);