0

I am using NSURLConnection in my app for downloading. It is working fine.When i start the connection the delegates methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse*)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connectionDidFinishLoading:(NSURLConnection*)connection

get called.

But when the number of connections become more then 4 then for the 5th connection the delegate methods do not called until the delegate method "connectionDidFinishLoading" is called for any of first four connections (means one of any four connection has finished). Then the delegates (didReceiveResponse and didReceiveData ) of Fifth connection start getting called .In short my NSURLConnection is delegate methods only called for 4 connections at a time.

I just want to know is there any way that i can increase this limit so that more then 4 (may be 8 or 9) connection's delegates get called at a time ? Is this a limit by iOS or something is wrong with my app.

Nilesh
  • 701
  • 5
  • 14
Abuzar Amin
  • 1,981
  • 1
  • 19
  • 33
  • Have you tested this on multiple servers and with different network infrastructure (different LAN and ISP, etc)? – trojanfoe Dec 01 '15 at 14:40
  • I would suggested you used [AFNetworking](https://github.com/AFNetworking/AFNetworking) instead of `NSURLConnection`. I think it would make you life easier. – rob180 Dec 01 '15 at 14:43

3 Answers3

2

Check http://blog.lightstreamer.com/2013/01/on-ios-url-connection-parallelism-and.html

iOS has a limit on the number of concurrent NSURLConnections to the same end-point? No? Maybe you should, we discovered it the hard way.

They also offer a thread pool implementation to get around this limit.

Ramon Poca
  • 1,889
  • 1
  • 10
  • 19
0

I think you should schedule these connections to a manual created queue other than the default one, so as to avoid the potential affect. It works well for me. Here are the sample code you can try:

NSURLRequest *request; // a URL request
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // manual create queue to schedule your URL connections
queue.maxConcurrentOperationCount = 10; // configure the max count

// by specifying NO for the #startImmediately# parameter, 
// you can schedule an connection on the queue you like before it starts.
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; 
[connection setDelegateQueue:queue];
[connection start];

Hope this will be helpful.

wzso
  • 3,482
  • 5
  • 27
  • 48
  • That won't help. The number of concurrent operations in the operation queue can, by definition, only reduce the number of concurrent NSURLConnection requests running in the queue, not increase it. – dgatwood Dec 04 '15 at 09:06
  • Never mind. I misread that and thought you were using a different (awful) technique with synchronous requests in operation queues. Your approach *might* work. It is still probably better to use NSURLSession, which not only gives you that support, but is actually documented by Apple as doing so. :-) – dgatwood Dec 04 '15 at 09:15
0

You can't fix it with NSURLConnection (practically), but if you use NSURLSession instead, you can trivially solve the problem.

The NSURLSession API lets you control the maximum number of concurrent requests per server. Just set the HTTPMaximumConnectionsPerHost property on the session configuration object, then create a session based on that configuration, then create tasks within that session much like you would create NSURLConnection objects.

dgatwood
  • 10,129
  • 1
  • 28
  • 49