MCKapur
MCKapur

Reputation: 9157

How to make -sendSynchronousRequest faster?

In my app I am performing multiple different methods, all on different threads, that download data from requests, parse it and add it to an array. Most of these methods download data from one request, but on the methods uses a for loop and downloads over 30 translations.

But, this delivers a bad user experience, the user has to watch a spinning wheel and read some silly randomly generated stalling text.

So what I want to know is how can I make synchronous requests (NSURLConnection) faster, which is the best technique? Should I immediately switch over to asynchronous since I am making so many requests? What does asynchronous even mean/do, will it help in this scenario?

To finish everything, the app can take up to 7 seconds on wifi and slowest 30 seconds on 3G (in Singapore, with the worst bandwidth however!)

So, how to improve? Any methods, techniques, api's, please help!

Thanks!

Upvotes: 1

Views: 618

Answers (2)

rmaddy
rmaddy

Reputation: 318794

Speed will not be affected in any way by using synchronous versus asynchronous. The Internet speed is the same either way.

The issue is that you must NEVER make synchronous networking calls on the main thread. Doing so locks up the user interface. Best case is your app appears locked up to the user (bad). Worse case, the OS kills your app for being unresponsive (bad).

All networking should be done in the background.

If your app can't really proceed until all of the network access is complete, at least you can put up a "busy" screen letting the user know what is going on. Perhaps use a progress bar to let the user know how far along the process is or show a "x of y" count for the number of files. What ever is appropriate to your app.

But again, the speed of the network access won't be changed. You will be providing a better user experience.

There are many questions here about how to do async file downloads. Many using NSURLConnection and others using 3rd party libraries that may make it easier depending on what you are doing. Search around a bit. Several are listed as related questions to this one.

Upvotes: 3

rooster117
rooster117

Reputation: 5552

The best way to fix this would be to us asynchronous calls. Beyond the speed you should avoid using synchronous requests whenever possible. Basically if you are using a synchronous but on a separate thread you are doing work that is already done with asynchronous. It will be more work setting it up but you can send all synchronous requests at the same time and they can finish at different times. There is a limit to how many http requests can be going at the same time but iOS will limit this for you and you should still see a massive performance improvement.

Upvotes: 3

Related Questions