Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

ios - setting limit on concurrent tasks in AFNetworking 2 running AFHTTPSessionManager

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...