I have an app that makes multiple URL requests to a data server in order to download user data. I'm trying to speed up the download time by making 10 requests in parallel. I've tried a couple different ways to do this, but the result I keep having is that although I send the 10 requests (nearly) simultaneously, I only receive data back from one at a time. This means that the total download time is the same as if I was making all the requests serially. Here are the different things I've tried.
- Created a subclass of NSOperation that has a NSURLConnection object and acts as the delegate for that NSURLConnection. Added 10 instances of this NSOperation subclass to a NSOperationQueue.
- Created 10 NSURLConnection objects and started them one after another. (With separate delegate objects so that I could keep the responses straight.)
Both cases have no problem downloading the data correctly, but neither of them will receive data as quickly as I'd expect. The time it takes for the server to respond to a request is about 0.5 - 1.0 second. So if the requests a truly going out in parallel, I would expect to receive all 10 responses back in at most 1 second. In both of the two approaches I've observed that the NSURLConnections are all being started within about 5 milliseconds, but the connect:didReceiveResponse methods for the requests get called one after another at about 0.7-second intervals as if the requests were being made serially.
Am I misunderstanding something about the way NSURLConnections or NSOperationQueues works? Any help would be appreciated. Thanks!
If you are making request to the same server with several NSURLConnection then the two way you implemented will hardly make any difference. But if you are making 10 connection to 10 different servers, it is possible that one or two of them are very slow and take time to respond the request then the second approach will take much time then first one. In the second approach the next request will sent only after you get response from the current one but in the first approach as the request are running parrallaly so the request will be completed as soon as it get response and the next will be queued. In this case both approach will give same result if maximumConcurrentOperation = 1.
Hope this will clear your doubt.