// 創(chuàng)建信號量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// 創(chuàng)建全局并行
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, ^{
// 請求一
[loginCode getUserInfoWithNick:nil andUserId:kUserId onSuc:^(id data) {
NSLog(@"yue");
dispatch_semaphore_signal(semaphore);
} andFail:^(NSError *error) {
}];
});
dispatch_group_async(group, queue, ^{
// 請求二
[CommodityViewModel getPriceTransformForIntegral:nil onSuccess:^(id data) {
NSLog(@"duihuan11");
dispatch_semaphore_signal(semaphore);
} onFailure:^(NSError *error) {
}];
});
dispatch_group_async(group, queue, ^{
// 請求三
[CommodityViewModel getPriceTransformForIntegral:nil onSuccess:^(id data) {
NSLog(@"duihuan22");
dispatch_semaphore_signal(semaphore);
} onFailure:^(NSError *error) {
}];
});
dispatch_group_notify(group, queue, ^{
// 三個請求對應三次信號等待
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//在這里 進行請求后的方法摹芙,回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI操作
});
});
2018/05/27上方dispatch_semaphore和dispatch_group其實是重復了的麦萤。使用dispatch_semaphore或者dispatch_group其中一個就夠了
dispatch_group使用
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//請求1
NSLog(@"第一個請求");
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//請求2
NSLog(@"第二個請求");
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//請求3
NSLog(@"第三個請求");
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//界面刷新
NSLog(@"回到主線程刷新頁面");
});
dispatch_semaphore使用
// 設定信號值=2 一次執(zhí)行兩個等待豹储,必須得等兩個等待結束后再次進入第三個等待
dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 任務一
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"111");
sleep(1);
NSLog(@"complete task 1");
dispatch_semaphore_signal(semaphore);
});
// 任務二
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"222");
sleep(1);
NSLog(@"complete task 2");
dispatch_semaphore_signal(semaphore);
});
// 任務三
dispatch_async(quene, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"3333");
sleep(1);
NSLog(@"complete task 3");
dispatch_semaphore_signal(semaphore);
});
dispatch_barrier_async使用
dispatch_queue_t queue = dispatch_queue_create("threadgcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"111");
});
dispatch_async(queue, ^{
NSLog(@"222");
});
dispatch_async(queue, ^{
NSLog(@"333");
});
dispatch_barrier_async(queue, ^{
for (int i = 0; i < 5000000; i++) {
if (50000 == i) {
NSLog(@"point");
sleep(5);
}
}
});
dispatch_async(queue, ^{
NSLog(@"444");
});