I have 100+ request.I need send a new request when the last one is done,so the server will not return error code - 429. How to make this by afnetworking 3.0?
-
try batch operation queue – Jigar Apr 18 '16 at 09:47
4 Answers
I'm not very familiar with the specific APIs of AFNetworking
, but you could setup:
- A variable array containing all your pending requests,
- A method called (e.g.)
sendNext()
that removes the first entry of the array, performs the request asynchronously, and inside the completion block, calls itself.
Of course, you will need a terminating condition, and that is simply stop when the array becomes empty.

- 16,006
- 8
- 81
- 189
-
thx.This is the way to solve the problem in my code.I'm looking for a better way by afnetworking. – lsdoy Apr 18 '16 at 09:45
-
It would be great if it has some sort of batch/queue functionality. I haven't used the framework though. – Nicolas Miari Apr 18 '16 at 09:59
There are 2 approaches that can deal with your problem.
Firstly, create an operation queue and add all requests to the queue. After that, create an operation of your new request, then add the dependency to all requests in the queue. As a result, your new operation (will execute the new request) will be performed after the last request is done.
Secondly, you can use dispatch_barrier_async, which will create a synchronized point on your concurrent queue. That means you should create a concurrency queue to execute your 100+ request, and that dispatch_barrier_async blocks in your custom queue will execute the new request.

- 1,224
- 11
- 17
Thanks Sendoa for the link to the GitHub issue where Mattt explains why this functionality is not working anymore. There is a clear reason why this isn't possible with the new NSURLSession structure; Tasks just aren't operations, so the old way of using dependencies or batches of operations won't work.
I've created this solution using a dispatch_group that makes it possible to batch requests using NSURLSession, here is the (pseudo-)code:
// Create a dispatch group
dispatch_group_t group = dispatch_group_create();
for (int i = 0; i < 10; i++) {
// Enter the group for each request we create
dispatch_group_enter(group);
// Fire the request
[self GET:@"endpoint.json"
parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
// Leave the group as soon as the request succeeded
dispatch_group_leave(group);
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
// Leave the group as soon as the request failed
dispatch_group_leave(group);
}];
}
// Here we wait for all the requests to finish
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// Do whatever you need to do when all requests are finished
});
I want to look write something that makes this easier to do and discuss with Matt if this is something (when implemented nicely) that could be merged into AFNetworking. In my opinion it would be great to do something like this with the library itself. But I have to check when I have some spare time for that.

- 1,801
- 1
- 14
- 29
This question possible duplicate of AFNetworking 3.0 AFHTTPSessionManager using NSOperation . You can follow @Darji comment for few call, For 100+ call add these utility classes https://github.com/robertmryan/AFHTTPSessionOperation/ . It is very impractical approach to sent 100+ request operation concurrently. If possible try to reduce it.