iOS for循环请求 之 使用GCD的dispatch_gro
2019-09-27 本文已影响0人
nickNic
业务如下有个数组里面装着许多产品ID,需要拿着产品ID去请求接口,该接口根据该产品ID来判断该产品是否可以上线,但该接口只能接受一个产品ID的传参,不接受多个产品ID的传参,所以只能是遍历产品ID数组,通过for循环一个一个的去请求,通过收到接口的反馈将能上线的产品ID装到一个数组里面,不能上线的产品直接剔除,最后将能上线的产品展示出来
//请求数据
- (void)requestData
{
//117 查询持仓。48091查询普通是否可担保 60 查询价格
__weak typeof(self) weakSelf = self;
NSMutableDictionary *mdicSendValue = [NSMutableDictionary dictionary];
NSMutableArray *ayStockData = [NSMutableArray array];
NSMutableArray *ayStockType = [NSMutableArray array];
NSMutableArray *ayRegx = [NSMutableArray array];
[mdicSendValue setTztValue:@"0" forKey:@"StartPos"];
[mdicSendValue setTztValue:@"1000" forKey:@"Maxcount"];
[mdicSendValue setTztObject:@"1" forKey:@"reqlinktype"];
[mdicSendValue setTztObject:@"0" forKey:@"tokentype"];
[[self requestHandle]requestWithSession:ZTRequestSessionJY AndSendValue:mdicSendValue action:@"117" successNo:-1 success:^(id dicResult, int nAction, NSDictionary *dicSendValue) {
//取可用资产信息
NSArray *ayAccount = [dicResult tztObjectForKey:@"GRID2"];
if(ayAccount.count>=2)
{
NSArray *aySpear = [ayAccount objectAtIndex:1];
// NSArray *aySpear = [strTemp componentsSeparatedByString:@","];
_headerView.tfAssetsValue.text = [aySpear objectAtIndex:2];
}
//取持仓信息
NSArray *ayGRID0Data = [dicResult tztObjectForKey:@"GRID0"];
NSMutableString *strCode =[NSMutableString string];
首先创建一个dispatch_group_t
dispatch_group_t downloadGroup = dispatch_group_create();
if(ayGRID0Data.count>=2)
{
for (int i = 1; i < ayGRID0Data.count; i++)
{
每次网络请求前先dispatch_group_enter
dispatch_group_enter(downloadGroup);
NSString *strTemp = [ayGRID0Data objectAtIndex:i];
NSArray *aySpear = [strTemp componentsSeparatedByString:@"|"];
ZTCalculationModel *model = [[ZTCalculationModel alloc]init];
model.codeValue = [aySpear objectAtIndex:1]; //遍历获取股票代码
model.num = [aySpear objectAtIndex:15]; //遍历获取可用数量
[self.arrList addObject:model];
NSString *strACCOUNTTYPE = [aySpear objectAtIndex:23];
if (strACCOUNTTYPE.length<=0)
{
strACCOUNTTYPE = @"SZACCOUNT";
}
NSString *sCode = [aySpear objectAtIndex:1];
if (sCode.length<=0)
{
sCode = @"";
}
NSMutableDictionary *dicSend = [NSMutableDictionary dictionary];
NSString *strToken = [[tztUserData getShareClass] getAccountTokenOfKind:0 tokenIndex:1];
if(strToken)
[dicSend setTztValue:strToken forKey:@"Token"];
else
[dicSend setTztValue:@"" forKey:@"Token"];
[dicSend setTztValue:sCode forKey:@"STOCKCODE"];
[dicSend setTztValue:strACCOUNTTYPE forKey:@"WTACCOUNTTYPE"];
[[self requestHandle]requestWithSession:ZTRequestSessionJY AndSendValue:dicSend action:@"48901" successNo:-1 success:^(id dicResult, int nAction, NSDictionary *dicSendValue) {
NSArray *ayData = [dicResult tztObjectForKey:@"GRID0"];
请求回调后再dispatch_group_leave,
dispatch_group_leave(downloadGroup);
//取可以担保信息
if(ayData.count>=2)
{
NSString *strTemp = [ayData objectAtIndex:1];
NSArray *aySpear = [strTemp componentsSeparatedByString:@"|"];
//遍历获取折算率 当股票代码和持仓代码一样 和拼接请求价格股票代码
[self.arrList enumerateObjectsUsingBlock:^(ZTCalculationModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj.codeValue isEqualToString:[aySpear objectAtIndex:1]])
{
[strCode appendFormat:@"%@|0,",[aySpear objectAtIndex:1]];
obj.rate = [aySpear objectAtIndex:3];
}
}];
}
} failure:^(int nErrNo, NSString *strErrMsg, int nAction, NSDictionary *dicSendValue, id dicResult) {
请求回调后再dispatch_group_leave,
dispatch_group_leave(downloadGroup);
}];
}
}
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
NSMutableDictionary *dicSend = [NSMutableDictionary dictionary];
[dicSend setTztObject:@"9" forKey:@"AccountIndex"];
[dicSend setTztObject:@"0" forKey:@"DeviceType"];
[dicSend setTztObject:@"1" forKey:@"Direction"];
[dicSend setTztObject:@"0" forKey:@"NewMarketNo"];
[dicSend setTztObject:@"1" forKey:@"Lead"];
[dicSend setTztValue:@"0" forKey:@"StartPos"];
[dicSend setTztObject:@"1" forKey:@"StockIndex"];
[dicSend setTztValue:@"100" forKey:@"MaxCount"];
[dicSend setTztObject:strCode forKey:@"Grid"];
[[self requestHandle]requestWithSession:ZTRequestSessionHQ AndSendValue:dicSend action:@"60" successNo:-1 originResponse:YES success:^(id dicResult, int nAction, NSDictionary *dicSendValue) {
[self GetHQRecevData:dicResult returnStocks_:ayStockData andStockTypes_:ayStockType];
[ayStockData enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.arrList enumerateObjectsUsingBlock:^(ZTCalculationModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
//遍历获取股票名字和股票代码拼接 股票价格 标记是否该股票 当股票代码和持仓代码一样
if ([model.codeValue isEqualToString:[[obj objectForKey:@"Code"]objectForKey:@"value"]])
{
model.codeValue = [NSString stringWithFormat:@"%@(%@)",[[obj objectForKey:@"Name"]objectForKey:@"value"],[[obj objectForKey:@"Code"]objectForKey:@"value"]];
model.price = [[obj objectForKey:@"NewPrice"]objectForKey:@"value"];
model.b_TPType = YES;
}
}];
}];
//遍历当标记为yes时那么就是我们要显示的数据保存起来 然后赋值给数据源
[self.arrList enumerateObjectsUsingBlock:^(ZTCalculationModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.b_TPType == YES)
{
[ayRegx addObject:obj];
}
}];
[self.arrList removeAllObjects];
[self.arrList addObjectsFromArray:ayRegx];
if (self.arrList.count>=3)
{
weakSelf.footerView.strMoreTitle = @"加载更多";
weakSelf.arrMark = 3;
}
else
{
weakSelf.footerView.strMoreTitle = @"添加担保证劵";
weakSelf.arrMark = self.arrList.count;
}
weakSelf.footerView.frameHeight = [_footerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
_tbMain.tableFooterView = _footerView;
[self.tbMain reloadData];
} failure:^(int nErrNo, NSString *strErrMsg, int nAction, NSDictionary *dicSendValue, id dicResult) {
}];
});
} failure:^(int nErrNo, NSString *strErrMsg, int nAction, NSDictionary *dicSendValue, id dicResult) {
}];
}
代码中我们只添加了4行代码
dispatch_group_t downloadGroup = dispatch_group_create();
dispatch_group_enter(downloadGroup);
dispatch_group_leave(downloadGroup);
dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
});
对以上4行代码可理解为:创建一个dispatch_group_t, 每次网络请 求前先dispatch_group_enter,请求回调后再dispatch_group_leave,对于enter和leave必须配合使用,有几次enter就要有几次leave,否则group会一直存在。当所有enter的block都leave后,会执行dispatch_group_notify的block。