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

来源:互联网 发布:星际淘宝网txt下载80 编辑:程序博客网 时间:2024/06/02 00:51

学了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内部的封装,我还要继续的去看,如果有所感悟,我会及时的分享给大家的。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 索尼镜头盖丢了怎么办 15-45镜头盖丢了怎么办 手机镜头盖丢了怎么办 三星微单镜头盖怎么办 大胸走路容易抖怎么办 哎呦该怎么办是什么歌 老婆想爆我菊花怎么办 衣服泡了一晚上怎么办? 来大姨妈了该怎么办 耳机戴不住总掉怎么办 眼睛被辣椒辣了怎么办 电信光猫复位后怎么办 电信光猫重置了怎么办 u盘无法安全弹出怎么办 u盘不能安全弹出怎么办 真空杯不保温了怎么办 锅底外面烧黑了怎么办 锅底里面烧黑了怎么办 佳能50镜头跑焦怎么办 g7x镜头进灰了怎么办 leica镜头进灰了怎么办 请问喉咙痒咳嗽该怎么办 喉咙痒咳嗽有痰怎么办 痒咳嗽停不下来怎么办 嗓子疼咳嗽有痰怎么办 上火了喉咙干痒怎么办 喉咙里总是有痰怎么办 1岁宝宝喉咙有痰怎么办 嗓子总感觉有痰怎么办 嗓子痒感觉有痰怎么办 1岁宝宝嗓子有痰怎么办 3岁宝宝嗓子有痰怎么办 喉咙总感觉有痰怎么办 感冒了喉咙有痰怎么办 咽喉有异物感是怎么办 老感觉喉咙有痰怎么办 感冒有痰怎么办最有效 感冒快好了有痰怎么办 喉咙里一直有痰怎么办 一到晚上就咳嗽怎么办 1岁宝宝咳嗽痰多怎么办