场景描述:我们同时发出了a、b、c 3个网络请求,我们希望在a、b、c 3个网络请求都结束的时候获得一个通知。
常见解决方法:通过度娘目前找到两种做法;1、通过添加标识来判断请求是否全部结束 2、dispatch_group + 信号量
本篇文章demo
1、添加标识的解决方法
在遇到这个问题时首先想到了唐巧大大的猿题库团队开源的网络框架ytknetwork,然后阅读源码发现ytknetwork是通过添加标识来实现网络请求的批量请求处理;
话不多说直接上代码在ytknetwork里负责进行网络批处理请求的是ytkbatchrequest类,下面看下它的使用示例:

ytkbatchrequest *batchrequest = [[ytkbatchrequest alloc] initwithrequestarray:@[a, b, c, d]];先调用初始化方法把4个网络请求的实例塞进去,看下这个初始化方法
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (id)initwithrequestarray:(nsarray )requestarray {
self = [super init];
if (self) {
_requestarray = [requestarray copy];
_finishedcount = 0;
for (ytkrequest req in _requestarray) {
if (![req iskindofclass:[ytkrequest class]]) {
ytklog(@"error, request item must be ytkrequest instance.");
return nil;
}
}
}
return self;
}
|
我们看到有一个_finishedcount的变量根据字面很好理解是用来记录请求完成的个数,然后我们全局搜下这个变量,发现只有在下面的这个方法中用到了这个变量
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- (void)requestfinished:(ytkrequest *)request {
_finishedcount++;
if (_finishedcount == _requestarray.count) {
[self toggleaccessorieswillstopcallback];
if ([_delegate respondstoselector:@selector(batchrequestfinished:)]) {
[_delegate batchrequestfinished:self];
}
if (_successcompletionblock) {
_successcompletionblock(self);
}
[self clearcompletionblock];
[self toggleaccessoriesdidstopcallback];
[[ytkbatchrequestagent sharedinstance] removebatchrequest:self];
}
}
|
上述方法是网络请求结束的回调代理方法,完成后_finishedcount计数加1,然后和保存网络请求实例的数组元素个数进行比较如果相等说明所有的请求都已经完成,调用回调的代理方法及block请求结束。
然后ytknetwork对于批量网络请求失败的处理是,只要一个失败就立即停止请求,调用失败回调:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (void)requestfailed:(ytkrequest )request {
[self toggleaccessorieswillstopcallback];
// stop
for (ytkrequest req in _requestarray) {//遍历请求实例数组
[req stop];//停止请求
}
// callback //回调
if ([_delegate respondstoselector:@selector(batchrequestfailed:)]) {
[_delegate batchrequestfailed:self];
}
if (_failurecompletionblock) {
_failurecompletionblock(self);
}
// clear
[self clearcompletionblock];
[self toggleaccessoriesdidstopcallback];
[[ytkbatchrequestagent sharedinstance] removebatchrequest:self];
}
|
总结:ytknetwork的做法大致就是用一个变量记录完成请求的个数,然后在单个网络请求结束回调的时候判断当前完成的网络请求个数是否和总的网络请求个数相等,如果相等则说明请求结束。
2、dispatch_group + 信号量
参考文章采用的是group + 信号量,下面示例采用dispatch_group_enter、dispatch_group_leave实现详见 本篇文章demo。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
- (void)loadrequest1
{
dispatch_group_t dispatchgroup = dispatch_group_create();
dispatch_group_enter(dispatchgroup);
[malafnmanger getdatawithurl:url1 parameters:nil finish:^(requestresult result) {
nslog(@"第一个请求完成");
dispatch_group_leave(dispatchgroup);
} des:@"第一个url"];
dispatch_group_enter(dispatchgroup);
[malafnmanger getdatawithurl:url2 parameters:nil finish:^(requestresult result) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(10);//网络请求结束后回调是在主线程如果sleep放在外面会阻塞主线程
nslog(@"第二个请求完成");
dispatch_group_leave(dispatchgroup);
});
} des:@"第二个url"];
dispatch_group_notify(dispatchgroup, dispatch_get_main_queue(), ^(){
nslog(@"请求完成");
});
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.jianshu.com/p/07eb268c93f2








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。