I’m writing an iPad app that pulls various coordinates from an XML file on a web server, parses the information, and renders them on the screen.
I was wondering if any of you have tips/suggestions on how I could have it refresh the data continuously, say, every second (since a computer program updates the XML file on the server every few seconds). Thanks!
You’re definitely going to need to implement a multi-threaded background operation for this, or your app will be entirely unresponsive. Before I go on, I must state that constantly querying a server for the app’s entire execution is a bad idea, not only will it chew through data, but having to keep the Wi-Fi/3G/4G antenna constantly alive will drain the user’s battery much faster.
I would consider relaxing the requirements to pulling the data down say every 10 seconds or so (since the server only updates it every few seconds and you have to factor in the time to download and render the currently grabbed co-ords), or even wait for the user to push a refresh button etc.
To answer the question as it stands, there’s a few ways of doing this, the easiest of which is probably using a framework like ASHTTPRequest (It’s no longer maintained though, for modern projects there’s libraries like AFNetworking). They handle the asynchronous download of data from a server amongst many other useful network functions.
The ideal process for your requirements would look something like:
performSelectorOnMainThreador equivalent!)Hope that helps! If you don’t want to include an external library, I did have a small sample project that creates instances of an subclassed NSOperation that performs an NSURLConnection, sends them to an NSOperationQueue and executes them concurrently. You could easily modify it to do only 1 operation at a time, change the NSURLConnection to download and parse the server data, and then update the main thread. It’s here if you’re interested.