Abuzar Amin
Abuzar Amin

Reputation: 1991

Fifth NSURLConnection's delegate not called

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.

Upvotes: 0

Views: 84

Answers (3)

dgatwood
dgatwood

Reputation: 10417

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.

Upvotes: 0

wzso
wzso

Reputation: 3895

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.

Upvotes: 0

Ramon Poca
Ramon Poca

Reputation: 1929

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.

Upvotes: 2

Related Questions