Limiting the number of concurrent NSURLConnection requests in iOS 5?

In iOS 5, only the application I'm working on seems to drop requests when I submit a large number of requests asynchronously. The big value is still pretty small - testing since 30 now

Each request uses its own NSURLConnection and is expected to be relatively fast (i.e. 300 ms - 2 s). We also keep one connection open for a long period of time.

It seems that every fifth connection cannot exit the device. Of course, it never reaches the server, and network debugging using Charles does not even display requests.

I am wondering if anyone knows about limiting the number of simultaneous open NSURLConnection objects and active queries?

It is worth noting that we do not see this problem in iOS 4. It also seems that if we kill our long-lived connection, we will no longer drop every fifth request.

+5
source share
1 answer

NSOperationQueueis the best way to deal with this problem. In short, you wrap NSURLConnectionin NSBlockOperation, and then add it to the queue. A queue allows you to set various properties, such as the maximum number of concurrent connections, and also give you an easy way to cancel operations in a queue.

This design template has a good introduction to WWDC (2012) video titled “Creating Parallel User Interfaces on iOS”.

In iOS 5, you can use the following call to launch NSURLConnectionand add it toNSOperationQueue

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

+4
source

All Articles