Question

I am confused on where to use which multithreading tool in iOS for hitting services and changing UI based on service data,

  1. firstly I got accustomed to using NSURLConnection and its delegates, used didreceiveresponse, didreceivedata etc delegates to achieve the task
  2. secondly I learned and used GCD to hit services and update the UI from within the block code
  3. Now I am learning to use performSelectorInBackground() to do work in background thread

Clearly confused on which tool to use where?

Was it helpful?

Solution

  1. NSURLConnection with delegate calls is "old school" way of receiving data from remote server. Also it's not really comfortable to use with few NSURLConnection instances in a single class (UIViewController or what not). Now it's better to use sendAsynchronousRequest.. method with completion handler. You can also define on which operation queue (main for UI, or other, background one) the completion handler will be run.
  2. GCD is good for different tasks, not only fetching remote resources with initWithContentsOfURL: methods. You can also control what type of queues will receive your blocks (concurrent, serial ,etc.)
  3. performSelectorInBackground: is also "old school" way of performing a method in background thread. If you weren't using ARC, you'd need to setup separate autorelease pool to avoid memory leaks. It also has a limitation of not allowing to accept arbitrary number of parameters to given selector. In this case it's recommended to use dispatch_async.

There are also NSOperationQueue with NSOperation and its subclasses (NSInvocationOperation & NSBlockOperation), where you can run tasks in the background as well as get notifications on main thread about finished tasks. IMHO They are more flexible than GCD in a way that you can create your own subclasses of operations as well as define dependencies between them.

OTHER TIPS

The most important thing is, that you never change UI anyway in another thread except the main thread.

I think, that all points you mentioned use the same technique in the background: GDC. But I'm not sure of that.

Anyway it doesn't matter which tool you should use in terms of threading.
It's rather a question of your purpose. If you wan't to fetch an small image or just few data you can use contentsOfURLin a performSelectorInBackground() or a GDC dispatch block.
If it's about more data and more information like progress or error handling you should stick with *NSURLConnection`.

I suggest using GCD in all cases. Other techniques are still around but mainly for backward compatibility.

GCD is better for 3 reasons (at least):

  1. It's extremely easy to use and the code remains very readable because of the use of blocks
  2. It is lower level than things like NSOperation so it is much faster when you need high performance multi threading
  3. It's lightweight and non-intrusive so your code doesn't have to change substantially when you want to add thread management in the middle of a method.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top