iOS开发-如何用GCD同步若干个异步调用?

如何用GCD同步若干个异步调用?(比如根据若干个url异步加载多张图片,然后在都下载完成后合成一张整图展示给用户)

使用Dispatch Group追加block到Global Group Queue,这些block如果全部执行完毕,就会执行Main Dispatch Queue中的结束处理的block。
代码如下

-(void)test
{
    //获取并发队列
    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, ^{

        NSLog(@"线程:%@--加载图片1", [NSThread currentThread]);
        });
    
    dispatch_group_async(group, queue, ^{
        
      NSLog(@"线程:%@--加载图片2", [NSThread currentThread]);;
    });
    
    dispatch_group_async(group, queue, ^{
        
        NSLog(@"线程:%@--加载图片3", [NSThread currentThread]);
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
      
      NSLog(@"线程:%@--合并图片", [NSThread currentThread]);
    });
    
}

你可能感兴趣的:(iOS开发-如何用GCD同步若干个异步调用?)