GCD 中组队列group与Afnetworking的结合使用

学了GCD以后,对所讲解的概念都有所了解了,但是对于group创建组队列,在组队列中是正确的使用AFnetworking发送网络请求。
下面分别来说一下吧。
1,我们使用 dispatch_group_enter(group);dispatch_group_leave(group);来解决
第一种情况,对某一个url多次请求的情况,

 dispatch_group_t group = dispatch_group_create();
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //Enter group

    for (int i =0; i<3; i++) {
        dispatch_group_enter(group);
        [manager GET:@"http://www.qq.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //Deal with result...
            //Leave group
            NSLog(@"***********   腾讯成功   %d ***********\n",i);
            dispatch_group_leave(group);
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //Deal with error...
            //Leave group
            NSLog(@"***********   腾讯失败   %d ***********\n",i);
            dispatch_group_leave(group);
        }];
        //More request...
    }




    dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
        NSLog(@"<><><><><><><><>   end   <><><><><><><><><><><><><><><><>");
    });

以前自己使用的时候都是单独创建一个global线程,在每个线程任务中加入网络请求,现在这样子写的话,减少了一层嵌套。

如果不是对某一个url多次请求的话,那么就需要

 dispatch_group_t group = dispatch_group_create();
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //Enter group
    dispatch_group_enter(group);
    [manager GET:@"https://www.baidu.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //Deal with result...
            //Leave group
        NSLog(@"***********   百度成功   ***********\n");
            dispatch_group_leave(group);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           //Deal with error...
            //Leave group
        NSLog(@"***********   百度失败   ***********\n");
            dispatch_group_leave(group);
    }];
    //More request...

    dispatch_group_enter(group);
    [manager GET:@"https://www.163.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //Deal with result...
        //Leave group
        NSLog(@"***********   网易成功   ***********\n");
        dispatch_group_leave(group);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //Deal with error...
        //Leave group
        NSLog(@"***********   网易失败   ***********\n");
        dispatch_group_leave(group);
    }];

    dispatch_group_enter(group);
    [manager GET:@"http://www.qq.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //Deal with result...
        //Leave group
        NSLog(@"***********   腾讯成功   ***********\n");
        dispatch_group_leave(group);
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //Deal with error...
        //Leave group
        NSLog(@"***********   腾讯失败   ***********\n");
        dispatch_group_leave(group);
    }];


    dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
        NSLog(@"<><><><><><><><>   end   <><><><><><><><><><><><><><><><>");
    });

2,我们使用GCD中的信号量来解决:
代码如下:

BaiduWeatherForCityDailyModel * model = [[BaiduWeatherForCityDailyModel alloc] init];
    //创建信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

    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, ^{
        [RequestManager getRecentlyThreeDaysWeather:@{@"location":location, @"language":@"zh-Hans", @"unit":@"c", @"start":@"0", @"days":@"3"} callBack:^(id data) {
            if (data) {
                NSArray* dataArray = (NSArray*)data;
                model.dailyModelArray = [dataArray mutableCopy];
            }
            dispatch_semaphore_signal(semaphore);
        }];
    });
    dispatch_group_async(group, queue, ^{
        [RequestManager getWeatherLive:@{@"location":location, @"language":@"zh-Hans", @"unit":@"c"} callBack:^(id data) {
            if (data) {
                NSArray* dataArray = (NSArray*)data;
                model.temperature = [dataArray firstObject];
                model.last_update = dataArray[1];
                model.weatherStatus = [dataArray lastObject];
            }
            dispatch_semaphore_signal(semaphore);

        }];
    });
    dispatch_group_notify(group, queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        callback(model);
    });

另外,关于AFnetworking内部的封装,我还要继续的去看,如果有所感悟,我会及时的分享给大家的。

你可能感兴趣的:(IOS)