0

Firstly, I'm extremely new to Cocoa and objective c development. In fact this is the first real project I'm working on - just a little Mac OSX app that does some web service calls.

I've obviously been Googling my fingers to the bone trying to figure out what's th best way to do this, ( or at least the most modern way) but can't really find anything. There's tons of blogs out there using a million different frameworks to get the job done but I won't know how dated all these ones are.

So, now, January 2014, what's the best way to do asynchronous web service calls in Cocoa?

3 Answers3

1

Its completely vary upon your requirement. But now days block mostly use for asynchronous task,

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
     //Do stuff of asynchronous task
    dispatch_async(dispatch_get_main_queue(), ^(void){
        //Update your UI.
    });

See doc of GCD

Also block method provide by NSURLConnection class ,

sendAsynchronousRequest:queue:completionHandler:

see here how to use it

If you want sync task with progress bar then you should go with NSURLConnectionDelegate methods

Now in newer version start new concept NSURLSession you go can with it.

Community
  • 1
  • 1
Tirth
  • 7,801
  • 9
  • 55
  • 88
1

The core answer to your question is NSURLSession.

There are simpler parts to do simpler thing, but NSURLSession is probably the best jumping-off point.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
1

If you want to make life easiest for yourself, use a third-party framework, AFNetworking.

If you are writing this code yourself (which I wouldn't necessarily suggest) you can use NSURLSession if supporting Mac OS X 10.9 and later only, or use NSURLConnection if you need to support prior versions.

For information on the Apple technologies, see the URL Loading System Programming Guide.


Personally, I've done it both ways. I have "rolled my own" set of classes which continue to evolve even now, more than a year later. I have also used AFNetworking, too. I learned a ton doing my own class framework, and pedagogically, that's a valuable exercise. But recognize that it takes a lot of effort to get up to speed on all of the subtleties of good asynchronous networking code.

Sure, one can use, for example, NSURLConnection method sendAsynchronousRequest easily enough, but if you want progress updates, handle authentication challenges, form application/x-www-urlencoded requests, use NSOperation-based requests, etc., you quickly find yourself in the non-trivial amount of code. AFNetworking is not without a few flaws, it's generally a very robust networking class that I heartily recommend for newer developers.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for the AFNetworking suggestion Rob. Is this what you use yourself? Not writing all that tedious code yourself isn't considered cheating is it? As long as you get the job done I guess.. –  Jan 27 '14 at 07:32
  • 1
    @Relborg I've done both. And no, using a third party framework is not "cheating", it's smart. And, sure, you can do your own simple requests with `NSURLConnection`/`NSURLSession` pretty easily. For anything other than the most trivial requests, though, to do it properly yourself quickly becomes a non-trivial exercise and I wouldn't recommend that for a new developer. AFNetworking boasts a significant user-base and is a good way to get going quickly without getting too lost in the details. – Rob Jan 27 '14 at 07:57