Reputation: 1477
I am currently using Grand Central Dispatch (iOS development) for image downloads from a remote server.
This entails dispatching a new thread from the main thread; the task of downloading the image from server is then happening on the side thread. When the task is completed, I obviously update the main thread view with the image.
My question is if the above method actually increases performance (download speed) by perhaps opening two TCP channels or if I am mainly performing a synchronous operation just on another thread, hence making it asynchronous, and thereby not making it any faster.
Based on my observations, the performance has stayed the same. Perhaps someone here can explain the "under the hood" theory and apply the theory to my concrete case above?
Upvotes: 2
Views: 498
Reputation: 12460
Apple go to great lengths to handle this at the operating system level and the "under the hood" implementation is constantly being optimised as time goes on. Manually managing sockets and connections for basic purposes may not be something you need to worry about - in fact your efforts may be undone by optimisations further along the line.
The general advice on iOS networking is to make sure you're using whatever the currently recommended networking frameworks is for your iOS version (currently NSURLSession and friends) and more importantly that you're using it correctly. This way you are automatically taking advantage of the networking optimisations the system is doing for you.
If you assume Apple has already implemented the best case networking on your behalf, your next options is to make sure you're fetching the minimum file size for your use case and your packets are travelling the minimum physical distance from your device to the image server.
If you're interested - There's heaps more in depth detail on iOS networking available - I would recommend watching Networking with NSURLSession from the 2015 WWDC conference and if you need convincing that Apple know what they're doing and are optimising the networking stack constantly definitely watch Your app and Next Generation Networks.
Upvotes: 4
Reputation: 4425
You will likely not see any performance change with downloading speed since the main issue is how fast the download executes and that depends on the network. Your logic inside an iOS app cannot block the main thread, so it needs to run on a background thread no matter what. Even if your image is very large, the decode time from PNG is going to be very fast once the whole image is downloaded. So, you are really just waiting for the download to complete. You are also not really doing much because iOS already uses pipelining of http requests behind the scenes, so multiple requests to the same address and port will likely be pushed through the same socket anyway. The only way you can really speed things up is to use more advanced graphics compression so as to reduce the total amount of data to download.
Upvotes: 7