iOS开发中GCD的dispatch_group_t的使用

在iOS的开发过程中,我们经常会碰到这样的需求,需要我们同时有多个网络请求,当多个网络请求全部完成后才能执行下一步的操作。例如最近一个需求:主营品牌(首先需要获取所有品牌,向A服务器去请求;其次获取某个门店的主营品牌,这次去B服务器请求,默认显示并选中它)
其实这个问题很简单,可以在A请求完成之后,再执行B请求即可,但是在codeReview过程中,一个开发人员提出了用GCD的dispatch_group_t即队列多线程来解决更好,确实,从理论上来说,用group组的方式确实会更合适,那就改吧。

    //创建组队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    //请求品牌数据
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [[CWNetManager shareManager] postRequestWithParamDictionary:dict
                                                        shouldCache:shouldCache
                                                          CacheTime:60*24*30
                                                           finished:^(id responseObj) {
                                                               dispatch_group_leave(group);
                                                               [self downloadSuccessWithResponse:responseObj];  
                                                           }
                                                             failed:^(NSError *error) {
                                                                 dispatch_group_leave(group);
                                                             }];
        
    });
    //请求主营品牌
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [[CWNetManager shareManager] postRequestWithParamDictionary:dict2
                                                        shouldCache:shouldCache
                                                          CacheTime:60*24*30
                                                           finished:^(id responseObj) {
                                                               dispatch_group_leave(group);
                                                              [self downloadIsMainBrandData:responseObj];
                                                           }
                                                             failed:^(NSError *error) {
                                                                 dispatch_group_leave(group);
                                                             }];
    });

//二个网络请求都完成统一处理
    dispatch_group_notify(group, queue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"这里写请求完成之后的处理");
        });
        
    });

这里有一个要注意的点
dispatch_group_enter(group);
dispatch_group_leave(group);

  • enter和leave要组队使用,每次enter都要有一次leave,否则group不会释放
  • 如果不使用enter和leave,调用顺序就会改变dispatch_async会在请求开始之后就调用。
    下面给另一个例子
-(void)Btn1{
    NSString *str = @"https://www.jianshu.com/u/4f749337a054";
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    
    for (int i=0; i<10; i++) {
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%d---%d",i,i);
        }];
        [task resume];
    }
    NSLog(@"end");
}

打印结果如下

2019-04-18 17:10:10.503 CWGCDDemo[3289:261033] end
2019-04-18 17:10:10.676 CWGCDDemo[3289:261080] 0---0
2019-04-18 17:10:10.704 CWGCDDemo[3289:261080] 1---1
2019-04-18 17:10:10.754 CWGCDDemo[3289:261096] 4---4
2019-04-18 17:10:10.760 CWGCDDemo[3289:261080] 2---2
2019-04-18 17:10:10.800 CWGCDDemo[3289:261096] 5---5
2019-04-18 17:10:10.840 CWGCDDemo[3289:261080] 7---7
2019-04-18 17:10:10.844 CWGCDDemo[3289:261082] 6---6
2019-04-18 17:10:10.846 CWGCDDemo[3289:261096] 3---3
2019-04-18 17:10:10.888 CWGCDDemo[3289:261096] 8---8
2019-04-18 17:10:10.945 CWGCDDemo[3289:261080] 9---9

接下来我们加上那两句enter和leave
dispatch_group_enter(group);
dispatch_group_leave(group);

-(void)Btn2{
    NSString *str = @"https://www.jianshu.com/u/4f749337a054";
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    
    dispatch_group_t downloadGroup = dispatch_group_create();
    for (int i=0; i<10; i++) {
        dispatch_group_enter(downloadGroup);
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%d---%d",i,i);
            dispatch_group_leave(downloadGroup);
        }];
        [task resume];
    }
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
        NSLog(@"end");
    });
}

这次的打印结果如下

2019-04-18 17:15:14.576 CWGCDDemo[3289:265942] 3---3
2019-04-18 17:15:14.626 CWGCDDemo[3289:265936] 2---2
2019-04-18 17:15:14.647 CWGCDDemo[3289:265944] 4---4
2019-04-18 17:15:14.648 CWGCDDemo[3289:265936] 0---0
2019-04-18 17:15:14.657 CWGCDDemo[3289:265943] 1---1
2019-04-18 17:15:14.709 CWGCDDemo[3289:265944] 5---5
2019-04-18 17:15:14.728 CWGCDDemo[3289:265944] 6---6
2019-04-18 17:15:14.734 CWGCDDemo[3289:265944] 7---7
2019-04-18 17:15:14.738 CWGCDDemo[3289:265943] 8---8
2019-04-18 17:15:14.816 CWGCDDemo[3289:265944] 9---9
2019-04-18 17:15:14.816 CWGCDDemo[3289:261033] end

这样大家就能看到区别了吧。

你可能感兴趣的:(iOS开发中GCD的dispatch_group_t的使用)