dispatch_group_async可以实现监听一组任务是否完成,完成后得到通知执行其他的操作。这个方法很有用,比如你执行三个下载任务,当三个任务都下载完成后你才通知界面说完成的了

  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, ^{
        
        [NSThread sleepForTimeInterval:1];
        NSLog(@"task1");
    });
    
    dispatch_group_async(group, queue, ^{
        
        [NSThread sleepForTimeInterval:2];
        NSLog(@"task2");
    });
    
    dispatch_group_async(group, queue, ^{
        
        [NSThread sleepForTimeInterval:3];
        NSLog(@"task3");
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        
        NSLog(@"updateUI");
        
    });

 

posted @ 2015-03-23 20:51  曹县三胖暴打大猩猩  阅读(202)  评论(0编辑  收藏  举报