在開發(fā)中遇到過這樣一個功能彻桃,某個界面列表上面是企業(yè)碎紊,下面是聯系人,而且獲取企業(yè)列表與聯系人列表的接口不是同一個至会,必須等企業(yè)跟聯系人列表數據都獲取完畢后才能刷新离咐,于是就用到了GCD組函數谱俭。
1487575235768582.png
然而問題來了奉件,[[IBOSServer shared]...]這玩意兒是封裝好的,并且是個異步函數昆著,而異步函數不會阻塞線程县貌,不會等里面的內容執(zhí)行完,就直接返回了凑懂,這就導致數據還沒拿到煤痕,就開始執(zhí)行notify里面的代碼了。這時需要使用另一種GCD組函數的用法接谨,完美解決問題摆碉。
1487575253598331.png
另外:
1、延時執(zhí)行
- (void)demo{
// 方法1 timer
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
// 方法2
[self performSelector:@selector(task) withObject:nil afterDelay:3];
// 方法3
/**
*參數1:延時時間 dispatch_time生成時間 納秒為計時單位 精度高
*參數2:隊列
*參數3:任務
*異步執(zhí)行
*/
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"task");
});
NSLog(@"over");
}
- (void)task{
NSLog(@"task");
}
2脓豪、模擬下載任務
/*1巷帝、下載 L01.zip
*2、下載 L02.zip
*3扫夜、通知UI:下載完成
1楞泼、2在3之前執(zhí)行
*/
- (void)demo1 {
// 串行驰徊,異步順序下載
dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒內的隨機時間
dispatch_async(serialQueue, ^{
NSLog(@"%@下載 L01.zip",[NSThread currentThread]);
});
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒內的隨機時間
dispatch_async(serialQueue, ^{
NSLog(@"%@下載 L02.zip",[NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@下載完成",[NSThread currentThread]);
});
});
});
}
3、并行異步
- (void)demo2 {
// 并行異步 "shift+command+o"快速查找
dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒內的隨機時間
// 并行同步順序下載
dispatch_sync(concurrentQueue, ^{
NSLog(@"%@下載 L01.zip",[NSThread currentThread]);
});
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 5秒內的隨機時間
dispatch_sync(concurrentQueue, ^{
NSLog(@"%@下載 L02.zip",[NSThread currentThread]);
});
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@下載完成",[NSThread currentThread]);
});
});
}
4堕阔、任務1棍厂、2在子線程上先順序執(zhí)行后,任務3超陆、4在主線程上執(zhí)行牺弹,最后任務5、6时呀、7在子線程上并發(fā)無序執(zhí)行
- (void)demo3 {
dispatch_queue_t serialQueue =dispatch_queue_create("serialQueue",DISPATCH_QUEUE_SERIAL);
dispatch_queue_t concurrentQueue =dispatch_queue_create("concurrentQueue",DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
dispatch_sync(serialQueue, ^{
NSLog(@"%@執(zhí)行任務1",[NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"%@執(zhí)行任務2",[NSThread currentThread]);
});
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@執(zhí)行任務3",[NSThread currentThread]);
});
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@執(zhí)行任務4",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
dispatch_async(concurrentQueue, ^{
NSLog(@"%@執(zhí)行任務5",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"%@執(zhí)行任務6",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^{
NSLog(@"%@執(zhí)行任務7",[NSThread currentThread]);
});
});
});
}
5例驹、隊列組
- (void)demo4 {
NSLog(@"begin");
// 創(chuàng)建隊列組
dispatch_group_t group =dispatch_group_create();
// 開啟異步任務
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
// 模擬網絡卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒內隨機時間
NSLog(@"%@下載 L01.zip",[NSThread currentThread]);
});
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
// 模擬網絡卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒內隨機時間
NSLog(@"%@下載 L02.zip",[NSThread currentThread]);
});
dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
NSLog(@"%@下載完成",[NSThread currentThread]);
});
}
6、GCD組函數的另外使用方式
- (void)demo5 {
/* dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)
{
dispatch_retain(group);
dispatch_group_enter(group);
dispatch_async(queue, ^{
block();
dispatch_group_leave(group);
dispatch_release(group);
}); */
dispatch_group_t group =dispatch_group_create();
dispatch_group_enter(group);
// 模擬異步網絡請求
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
//模擬網絡卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒內隨機時間
NSLog(@"%@下載 L01.zip",[NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
// 模擬異步網絡請求
dispatch_group_async(group,dispatch_get_global_queue(0,0), ^{
// 模擬網絡卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)]; // 休眠5秒內隨機時間
NSLog(@"%@下載 L02.zip",[NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_notify(group,dispatch_get_global_queue(0,0), ^{
NSLog(@"%@下載完成",[NSThread currentThread]);
});
}