利用AFN3.0进行网络请求,要发送的参数在一个数组里,每次取数组中的一条数据设置参数发送请求,
我通过for循环遍历数组并设置参数发送请求,将网络请求返回的数据添加到一个新的数组中,
发现不是每次请求返回的数据的顺序是一样的,功能需求返回数据的排序必须跟发送请求的顺序是一样的,
求问有什么方法保证前一次请求返回数据之后再发送下一次请求?
非常感谢,万分感谢!!!!
/**
* 加载订单数据,设置控件位置
*/
- (void)loadOrderData {
NSString *userID = [[NSUserDefaults standardUserDefaults] stringForKey:@"GOId"];
NSDictionary *dict = @{
@"GOId" : userID,
};
// 将字典转为json
NSDictionary *params = [ELHOCToJson ocToJson:dict];
NSString *URL = [NSString stringWithFormat:@"%@RealtimeOrder_getROListCon12345.action", ELHBaseURL];
__weak typeof(self) weakSelf = self;
[self.manager POST:URL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
weakSelf.ELHOrderArray = [ELHOrderModel mj_objectArrayWithKeyValuesArray:responseObject];
// 获取订单编号
for (int i = 0; i < weakSelf.ELHOrderArray.count; i++) {
ELHOrderModel *model = weakSelf.ELHOrderArray[i];
[weakSelf.ELHOrderNumArray addObject:model.ROBM];
}
// 根据获取到的订单编号加载订单详情列表
if (weakSelf.ELHOrderNumArray != nil) {
dispatch_queue_t conCurrentQueue = dispatch_queue_create("order", NULL);
for (NSString *ROBM in weakSelf.ELHOrderNumArray) {
dispatch_barrier_async(conCurrentQueue, ^{
// 加载订单详情列表
[self loadOrderDetailData:ROBM];
});
}
}
// 刷新数据
[orderVC.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
}
/**
* 加载订单详情列表
*/
- (void)loadOrderDetailData:(NSString *)ROBM {
NSDictionary *dict = @{
@"ROBM" : ROBM,
};
// 字典转json
NSDictionary *params = [ELHOCToJson ocToJson:dict];
NSString *URL = [NSString stringWithFormat:@"%@OrderPrice_getOPListByROBM.action", ELHBaseURL];
__weak typeof(self) weakSelf = self;
[self.manager POST:URL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 将获取到的订单详情数据逐个添加到数组中
[weakSelf.ELHOrderDetailArray addObject:[ELHOrderDetailModel mj_objectArrayWithKeyValuesArray:responseObject]];
ELHOrderTableViewController *orderVC = weakSelf.childViewControllers.firstObject;
orderVC.orderDetailArray = weakSelf.ELHOrderDetailArray;
// 刷新数据
[orderVC.tableView reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
}
Of course it may not be the same according to your approach. Some requests may be issued later, but the return is faster, so they are returned first.
I’m not sure whether your needs must be requested in order. For example, if we upload 10 pictures at a time and then stuff the returned URLs into an array in order, we usually request them in several threads at the same time instead of passing them one by one. This is much faster.
If you want to ensure that requests are made in sequence, you must send the next request after the end of the previous request. For example:
That’s probably what it means, do you understand?
But like uploading 10 pictures, as long as the order of the results is ensured and there is no need to request serialization one by one, you should use
[AFURLConnectionOperation batchOfRequestOperations:]
. It's best to prioritize whether this meets your needs.Your question is similar to this one https://segmentfault.com/q/1010000004632... If you just want to ensure the order of the returned results, you don't necessarily need all requests to be performed serially.
In addition, if you must make requests serially, you can create a new NSOperationQueue, set its maxConcurrentOperationCount to 1, and then add each of your requests to this NSOperationQueue sequentially.
You can use GCD dispatch_barrier_async, but the queue must be created by yourself, not the global queue
I wrote a new auxiliary class, I hope it can help you.
https://github.com/sunbohong/SunOrderArr...