darksky
darksky

Reputation: 21019

Using GCD or not?

I have an iPhone app which pretty much is a mobile app for a website. Pretty much everything it does is call API methods from our server. The app retrieves the user's information, and keeps updating the server using the API.

My colleague and I had a discussion whether to introduce GCD to the downloading aspect on the app. My colleague argues that since the UI needs to wait for the download to complete before it can display the pictures, text or whatever, there is absolutely no need for GCD. My argument is that we should keep the main thread busy with UI rendering (even if there is no data), and introduce GCD to the app to create other threads for download.

Which argument is right here? In my case, if the UI renders with no data, will there be some sort of lag? Which will yield a cleaner, sleeker and faster app?

Upvotes: 1

Views: 336

Answers (4)

Kris Subramanian
Kris Subramanian

Reputation: 1900

Here is a good read on using NSOperationQueues (that uses GCD behind the scenes) to download images. There is also some Github code you can check out. They have an elegant solution to pause downloads and enqueue new downloads when the user moves to different parts of your app.

http://eng.alphonsolabs.com/concurrent-downloads-using-nsoperationqueues/?utm_medium=referral&utm_source=pulsenews

Use GCD / NSOperationQueues as opposed to using NSThreads. You will have a good learning on core fundamentals and at the same time create a well architectured app. :)

Upvotes: 0

Wolfgang Schreurs
Wolfgang Schreurs

Reputation: 11834

A more appropriate question would perhaps be if you should download asynchronously or on the main thread for your app, since there are several different methods to download asynchronously on iOS (e.g. using NSThread, NSOperation or indeed GCD). An easy approach to achieve your goals could be to use the AFNetworking library. It makes multithreaded networking / internet code very easy to implement and understand.

Personally I'm very fond of GCD and recommend you learn it someday soon, though in itself it is not as suitable for asynchronous downloading compared to a library like AFNetworking (that uses GCD under the hood, I believe).

Upvotes: 1

fvwmer
fvwmer

Reputation: 301

I don't think doing time consuming operations in the main thread is a good idea.

Even if user have to wait for the data te be downloaded before he can do anything meaningful, still he will not hope UI is blocked.

Let's assume you have a navigator view, and after user tap some button, you push a new view to it and start download something. If user suddenly decides he don't want to wait anymore, he tap the "back" button. If your downloading operation blocks UI, user will have to wait it to end, it's really bad.

Upvotes: 2

werner
werner

Reputation: 859

One argument would be : what will happen when the download fails and times out because there is a problem at the server end ?

  • Without GCD the app will remain blocked and will crash after a time out since the UI can not be blocked for longer than 20 seconds.
  • With GCD the application remains functional but there will be no data being downloaded and the application will not crash.

Other aspects to take into account are :

  • the thread safety of the objects that you are using
  • how you handle downloads that are no longer necessary because the user navigates away from the page

Upvotes: 4

Related Questions