ios GCD 多网络请求成功 后刷新页面,与AFNetwork
2019-02-20 本文已影响17人
荔枝lizhi_iOS程序猿
1.开胃小菜
怎么用GCD 下载图片呢? ,并且下载的时候显示loading ,这就要异步下载,且不阻塞主线程。
// 开启一个线程进行数据请求
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url1 = [NSURL URLWithString:@"http://pic.qiantucdn.com/58pic/18/32/08/32658PICPUs_1024.png"];
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:url1];
if (data != nil) {
// 在主线程中完成UI操作
dispatch_async(dispatch_get_main_queue(), ^{
[self.indicator stopAnimating];
self.indicator.hidden = YES;
UIImage *image = [UIImage imageWithData:data];
self.imgV.image = image;
});
}else {
NSLog(@"error whin download:%@",error);
}
});
2. GCD 多个网络请求
在开发中经常遇到 ,一个页面有多个网络请求,要在多个网络请求之后再刷新UI,这个问题该怎么解决呢。
方法一 : 用CGD Group 实现,请看代码。
self 循环引用 请忽略,不是本文章的重点。
-(void)requestAll_GCDGroup{
// 方法一: GCD的leave和enter 我们利用dispatch_group_t创建队列组,手动管理group关联的block运行状态,进入和退出group的次数必须匹配。
//1.创建队列组
dispatch_group_t group = dispatch_group_create();
//2.创建队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//3.添加请求
dispatch_group_async(group, queue, ^{
dispatch_group_enter(group);
[self requestTopWithSuccessCallBack:^(NSArray *array) {
self.topArr = array;
dispatch_group_leave(group);
} failCallback:^(bool isFail) {
dispatch_group_leave(group);
}];
});
dispatch_group_async(group, queue, ^{
dispatch_group_enter(group);
[self requestShehuiWithSuccessCallBack:^(NSArray *array) {
self.shehuiArr = array;
dispatch_group_leave(group);
} failCallback:^(bool isFail) {
dispatch_group_leave(group);
}];
});
dispatch_group_async(group, queue, ^{
dispatch_group_enter(group);
[self requestGuoneiWithSuccessCallBack:^(NSArray *array) {
self.guoneiArr = array;
dispatch_group_leave(group);
} failCallback:^(bool isFail) {
dispatch_group_leave(group);
}];
});
//4.队列组所有请求完成回调刷新UI
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// NSLog(@"model:%f",_buyingStrategyModel.leverrisk);
NSLog(@"加载完成");
[self.mainTable.mj_header endRefreshing];
[self.mainTable reloadData];
});
}
方法二: GCD的信号量dispatch_semaphore_t
这种方式有点类似于通知模式,是利用监听信号量来发送消息以达到并发处理的效果,我们来看看代码:
//方法 二:
-(void)requestAll_GCDSemaphore{
//创建信号量
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, ^{
//1
[self requestTopWithSuccessCallBack:^(NSArray *array) {
self.topArr = array;
dispatch_semaphore_signal(semaphore);
} failCallback:^(bool isFail) {
dispatch_semaphore_signal(semaphore);
}];
});
dispatch_group_async(group, queue, ^{
//2
[self requestShehuiWithSuccessCallBack:^(NSArray *array) {
self.shehuiArr = array;
dispatch_semaphore_signal(semaphore);
} failCallback:^(bool isFail) {
dispatch_semaphore_signal(semaphore);
}];
});
dispatch_group_async(group, queue, ^{
//3
[self requestGuoneiWithSuccessCallBack:^(NSArray *array) {
self.guoneiArr = array;
dispatch_semaphore_signal(semaphore);
} failCallback:^(bool isFail) {
dispatch_semaphore_signal(semaphore);
}];
});
dispatch_group_notify(group, queue, ^{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"信号量为0");
dispatch_async(dispatch_get_main_queue(), ^{
// 通知主线程刷新 神马的
[self.mainTable.mj_header endRefreshing];
[self.mainTable reloadData];
});
});
}
参考 https://blog.csdn.net/pianzhidenanren/article/details/52571853
三.效果demo源码
下面是demo 源码地址,可以运行的项目。有需要的同学可以看下。
demo 效果图:
image.png
内页-实现页
image.png
头条是一个网络请求,
社会是一个网络请求
国内是一个网络请求。 共三个请求。三个请求都结束的时候 ,结束下拉刷新。
参考:https://blog.csdn.net/lee_woxinyiran/article/details/80790578
demo地址 https://gitee.com/Sunny0123/GCDTest