views:

56

answers:

3

I am just testing an app to get data off our web server, previously I had been using:

NSURL, NSURLRequest, NSURLConnection etc. to get the data that I wanted.

But I have just noticed that if I swap to using XML I can simply do the following and pass the results to NSXMLParser:

NSURL *url = [NSURL URLWithString:@"https://www.fuzzygoat.com/turbine?nbytes=1&fmt=xml"];

Am I right in thinking that if your just after XML this is an acceptable method? It just seems strongly short compared to what I was doing before?

gary

+1  A: 

That code only creates a URL object that represents a URL. It doesn't make any request or download any data. You still need to use NSURLRequest and NSURLConnection in order to actually download any data from the server.

Also, stay away from methods like 'initWithContentsOfURL:` and friends, unless you understand that they will block the thread that they are called on until complete. For network services, this method shouldn't be used because it'll block your UI for an indeterminate time, because you can't predict how fast the internet connection will be wherever the app is used.

NSURLConnection's asynchronous request system is exactly what you need. It won't block the UI, and provides a nice encapsulated interface to downloading data from a remote location.

Jasarien
fuzzygoat
I assume you mean `NSXMLParser`, not `NSURLParser`? If so, then that's true, it will download the contents of the URL and parse it, but it's not a good way to do it. This is because calling `initWithContentsOfURL:` on NSXMLParser is a synchronous call and will block for up to a minute if the XMLParser can't reach the URL for any reason.
Jasarien
Sorry my mistake NSXMLParser. I think I know what to do now. Use NSURLRequest and then NSURLConnection to get response NSData, I can then feed that into NSXMLParser using initWithData: does that sound more like it?
fuzzygoat
Don't use NSXMLParser or NSURLConnection as they are too memory intensive and contain internal leaks.
coneybeare
Hi coneybeare, and the alternative would be?
fuzzygoat
see my answer. ASIHTTPRequest and a different XML Parser
coneybeare
That's the right idea fuzzygoat, and as coneybeare says, `ASIHTTPRequest` is a great way to do url requests, just remember to be careful around the `initWithContentsOfURL:` type methods.
Jasarien
A: 

Depending on how much XML you get back from the web service, NSXMLParser may not be ideal because the entire XML document has to be read into memory.

Memory is pretty scarce on an iPhone, so using a SAX parser like that in libxml2 is probably better for larger XML files. Instead of reading the entire document into memory, the XML is streamed through and parsed for specific nodes of interest. The memory overhead is lower because less data is stored at once.

Once a node of interest is parsed, an event handler is called to do something useful, like store the node data somewhere.

In this case, take a look at Apple's XMLPerformance sample project for example code.

Alex Reynolds
The amount of data is actually pretty small, its about 3-4k of data.
fuzzygoat
Its a very small amount of data and using NSXMLParser is probably a bit overkill but I just wanted to do it right for future reference. Prior to using XML I was just "screen scraping", so this is certainly more elegant.
fuzzygoat
+1  A: 

This is definitely the right way to go. There do exist many different connection methods (including my favorite, ASIHTTPRequest) and many, many different xml parsers (including my favorite, KissXML) that are faster or more memory efficient than the Apple built in methods.

But to answer your question, yes, your logic and design pattern is correct.

UPDATE: Because Jasarien seems to think the question talks about asynchronous actions, I will discuss that here. ASIHTTPRequest handles async very very easily. Just check out the quick samples.

coneybeare
I completely disagree. Unless you set up your own thread management, this method will block until complete, which is not acceptable when accessing network resources. If the URL returns a large XML document, and the user running the app is on an EDGE or even GPRS connection, this method could block for a number of seconds, which will give the impression that the app is sluggish and unresponsive.Using asynchronous APIs wherever possible when dealing with network resources, like `ASIHTTPRequest` that you mention is "definitely the right way to go", as you put it.
Jasarien
I forgot to mention that when using these synchronous methods, that the default timeout is 1 minute, which means if for any reason the network resource is unreachable, the app will freeze for 1 minute before resuming, and the user definitely won't be happy about that.
Jasarien
I think you should revert your downvote. The question was not about asynchronous or synchronou, but about how to to get and parse xml. Undoubtedly async is the right way to go, but that is a different question.
coneybeare
But that is what the question is asking: is it acceptable to just init an xml parser with a url? No. That will cause the problems I described above. Unless you're already multithreading and don't want to add NSURLConnection's asynchronosity into your mix, this isn't the right way to do it.
Jasarien