//創建信號量
dispatch_semaphore_tsemaphore =dispatch_semaphore_create(0);
//創建一個隊列組
dispatch_group_tgrouch =dispatch_group_create();
//創建全局隊列,四個請求同時發起
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//創建串行隊列,四個請求依次執行
//dispatch_queue_tqueue =dispatch_queue_create("queues",NULL);
dispatch_group_async(grouch, queue, ^{
NSLog(@"網絡請求a");
dispatch_semaphore_signal(semaphore);//釋放通知
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"網絡請求b");
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"網絡請求c");
dispatch_semaphore_signal(semaphore);
});
dispatch_group_async(grouch, queue, ^{
NSLog(@"網絡請求d");
dispatch_semaphore_signal(semaphore);
});
//在所有請求都結束後,接到通知執行下面的方法
dispatch_group_notify(grouch, queue, ^{
//發起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//發起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//發起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
//發起等待
dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER);
NSLog(@"4個請求結束之後,可以在這裡操作請求的數據,或者更新UI");
});
【IOS中多個網絡請求問題】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!