16

so I know that in the old AFNetworking this was possible using the AFHTTPClient, and I know that if I use AFHTTPRequestOperationManager I can set the queue's limit, but I can't make AFHTTPSessionManager to run only x requests at a time without implementing it by myself using the success block (which I don't want to).

The following code did NOT limit my connections:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.operationQueue.maxConcurrentOperationCount = 1;

In line with an interesting discussion here, I have a lot of requests to my server and I choke it until I get timeouts, so I really need to limit my concurrent connections.

What am I missing?

shaioz
  • 340
  • 3
  • 12

2 Answers2

23

AFHTTPSessionManager uses tasks instead of operations (NSURLSessionDataTask, specifically), which is why you can't set an operation queue.

As you can see in the implementation of this class, tasks are immediately started ([task resume]) and not added to any sort of queue.

Consequently, and unfortunately, there is no built-into-AFNetworking way to set a limit to the number of concurrent tasks using AFHTTPSessionManager.

Possible alternatives:

  1. Use AFHTTPRequestOperationManager instead (this is what I'm doing)
  2. Build an NSOperation subclass that has a task as a property, and start the task in the [operation start] method of your subclass
  3. Create a Grand Central serial queue and create and start tasks in this queue
  4. If your requests are all to the same host, directly access the HTTPMaximumConnectionsPerHost option in the foundation URL loading system, like so:

    [NSURLSessionConfiguration defaultSessionConfiguration].HTTPMaximumConnectionsPerHost = 4;
    

    This approach has a number of caveats, which are discussed in the Apple documentation.

If you wind up doing #2, please submit it as a pull request to AFNetworking - it would be a welcome addition.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks you for the detailed answer, I ended up doing #1, which was the simplest. – shaioz Jan 30 '14 at 13:16
  • @LeszekS Still the same. – Aaron Brager Dec 11 '14 at 20:59
  • @AaronBrager Will there be an option to set the # of maximum concurrent operations on `AFHTTPSessionManager` in **AFNetworking 3** since `AFHTTPRequestOperationManager` is removed? – adamup Nov 19 '15 at 21:55
  • @adamup [Doesn't look like it.](https://github.com/AFNetworking/AFNetworking/blob/1272e3e32dbd447d18d75bc35e7a2f0f04f33d61/AFNetworking/AFHTTPSessionManager.m#L115) – Aaron Brager Nov 19 '15 at 22:08
  • If you still need maxConcurrentOperationCount in AFNetworking 3 then you can check my fork https://github.com/fins/AFNetworking and use AFHTTPOperationSessionManager instead of AFHTTPSessionManager. It wraps tasks in NSOperations so you can set requestsOperationQueue.maxConcurrentOperationCount to anything you need :) If you are using pods then you can just edit podfile like this: `pod 'AFNetworking', :git => 'https://github.com/fins/AFNetworking.git'` – Leszek Szary Aug 18 '16 at 20:32
3

You can configure AFHTTPSessionManager NSURLSessionConfiguration:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.HTTPMaximumConnectionsPerHost = 2;

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
Brody Robertson
  • 8,506
  • 2
  • 47
  • 42