Reputation: 614
I just wanted to clarify my understanding of web services consumed in iOS apps.
So, NSURLConnection is used to invoke a web method from an iOS app and to get the data returned by the web method. And once the data is received, we can use either XMLParser or JSON parser to parse that data, depending on what the web service is written in. Is that correct?
Upvotes: 0
Views: 2554
Reputation: 1892
Just to add onto what others have already said in the comments:
Ideally you'll want to always connect asynchronously, using NSURLConnection's Delegate which will not block the main thread. If you're not already aware, iOS will force quit applications that block the main thread for too long a time. Synchronous connections can be ok in certain instances, but I'd say over 90% of the time you'll want asynchronous.
That said, asynchronous connections cause their own set of headaches. If you need to have data before allowing your users access to the application interface, you'll need to throw up loading screens and then remove them. You'll need to manage canceling requests if the user moves to a different part of the application that starts a new request. You'll also want to determine if any ongoing requests need to be registered for background completion in the event the user backgrounds the app.
On the JSON parsing side of things, it's always recommended to use NSData when you can, as converting JSON from NSString adds a lot of overhead from NSString itself. I personally do NOT use Apple's provided JSON parser, but the excellent JSONKit as it's faster than even Apple's binary plist parser. It does however require that your JSON adhere very strictly to the JSON RFC, and that it be UTF-8/16/32 encoded, not ASCII. This is fairly standard amongst the faster JSON parsers available.
Avoid XML all together when possible. The built-in iPhone parser is a SAX-style parser only, which is obnoxious to implement. If you must use XML, take a look at Ray Wenderlich's XML parser comparison and choose an appropriate one. If you have a large XML document to parse though, SAX is probably your only option given the limited processing capabilities of iDevices.
--EDIT--
One of the commenters mention SBJSON. I'd recommend against that entirely. It's slower than Apple's JSON parser (which has been available since iOS 5), and is much slower then JSONKit and several others by an order of magnitude. I specifically moved my current enterprise-level iOS Application off of SBJSON because of errors I was receiving in the JSON parsing as well.
Upvotes: 3