我们经常会遇到一起请求多个网络数据的情况…有些操作是在所有的网络数据请求结束之后才可以进行的….比如说使刷新控件收回.或者某些UI控件的更新..这种情况就不能单纯的在某一条网络请求结束后的block里操作了.一涉及到异步,GCD的强大之处就体现出来了..先上代码.
//信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//创建全局并行
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//网络请求一
dispatch_group_async(group, queue, ^{
[self httpGetMyProjectContentSuccess:^{//网络请求完成
dispatch_semaphore_signal(semaphore);
} Error:nil];
});
//网络请求二
dispatch_group_async(group, queue, ^{
[self httpGetTaskReplyListSuccess:^{//网络请求完成
dispatch_semaphore_signal(semaphore);
} Error:nil];
});
dispatch_group_notify(group, queue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
网友评论