Reputation: 4652
I am confused on where to use which multithreading tool in iOS for hitting services and changing UI based on service data,
Clearly confused on which tool to use where?
Upvotes: 1
Views: 490
Reputation: 5766
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 contentsOfURL
in 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`.
Upvotes: 1
Reputation: 21902
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):
Upvotes: 0
Reputation: 49344
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.initWithContentsOfURL:
methods. You can also control what type of queues will receive your blocks (concurrent, serial ,etc.)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.
Upvotes: 2