利用AFNetworking进行网络请求,要发送的参数在一个数组里,每次取数组中的一条数据设置参数发送请求,
我通过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 success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
weakSelf.ELHOrderArray = [ELHOrderModel mj_objectArrayWithKeyValuesArray:responseObject];
dispatch_queue_t conCurrentQueue = dispatch_queue_create("order", NULL);
// 获取订单编号
for (int i = 0; i < weakSelf.ELHOrderArray.count; i++) {
ELHOrderModel *model = weakSelf.ELHOrderArray[i];
[weakSelf.ELHOrderNumArray addObject:model.ROBM];
dispatch_barrier_async(conCurrentQueue, ^{
// 加载订单详情列表
[self loadOrderDetailData:model.ROBM]; // 问题可能就出在这个for循环里,发送请求的顺序应该是按顺序发送的,但是返回数据的时间可能就不一样了,所以造成了数据顺序错乱的问题,求问如何解决此问题?
});
}
// 刷新数据
[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 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);
}];
}
I found a compromise, which is to use the
dispatch_group_async
function in the callback of afnetworking for sequence control.The code is as follows:
After reading the document,
operationQueue
is used to handledelegate
callbacks.NSURLSessionConfiguration
'sHTTPMaximumConnectionsPerHost
can guarantee Only one request is initiated under the same domain name at a time, the code is as follows:operationQueue
是用来处理delegate
回调的,NSURLSessionConfiguration
的HTTPMaximumConnectionsPerHost
可以保证同一域名下每次只发起一个请求, 代码如下:不过
AFNetworking
使用了并行队列url_session_manager_processing_queue()
处理数据, 由于不能保证这个队列的先后性, 所以其实也不能够确保回调的串行性----------------------------------------原答案-----------------------------
把
manger
里queue
的maxConcurrentOperationCount
设置成 1.还有你这里的
However,dispatch_barrier_async
我不知道你是想干嘛, 保证串行? 你这个for
rrreeeAFNetworking
uses a parallel queueurl_session_manager_processing_queue()
to process data. Since the sequence of this queue cannot be guaranteed, the seriality of callbacks cannot be guaranteed. #🎜🎜# #🎜🎜#----------------------------------------Original answer---- ------------------------Put the
of
is set to 1.#🎜🎜# #🎜🎜#And yourqueue
inmanger
maxConcurrentOperationCountdispatch_barrier_async
here, I don’t know what you want to do, ensure seriality? Yourfor
loop is inherently serial#🎜🎜 #First suspend the sending queue, and then when the for loop generates requests, add dependencies between the requests and put them in the queue.
There is still no way to determine the order of the returned data, I feel...
I also encountered this problem. Did you solve it? How did you do it