views:

1237

answers:

2

I'm new to iPhone app development so excuse me if I use the wrong terminology, or ask the wrong questions. Basically, I'm trying to write an App that includes downloading a dataset, saving it for later, and checking if the dataset has been updated, but I don't know where to begin.

When I say dataset, I mean a multi-dimensional array of key/value pairs.

I will be creating a site that my App will pull the data from. I think REST is the technology that it will be using (new to REST, too), being served up by a Zend Framework application, using MySQL as the back end database.

So the data will be stored in a MySQL database, and I need to be able to download chunks, which I assume will be stored in my App's SQLite database, to be accessed later (when internet access is not present). At some point in my application's life cycle, I want to check if the dataset that I have downloaded is the most recent version.

There are a lot of parts to this that I am still confused about. Can anyone please shed some light on any/all of the areas that I have touched upon. Is there any iPhone framework that I should be aware of that would make this process go faster/easier?

Thanks!

Update: Maybe I should break this up to make it easier to answer:

  1. I am assuming that my API will retrieve the data from the database and output the response as XML. Is this the best/only option?
  2. In my App, how can I make an API call?
  3. How can I parse the response (probably XML) from an API call and store it for later retrieval?
+1  A: 

If just wanting to fetch (GET) data, a REST API call is simply a URL you go to that usually returns XML instead of HTML. Your URL could be www.site.com/rest/fetchstuff or www.site.com/dosomethigncool, doesn't matter. For the app to "call" the API, it'd connect to that URL and include whatever parameters then download the XML from the server.

Let's say your app finds cars for sale. The URL/REST API would be www.site.com/rest/cars/findAll. Your app would call this URL: www.site.com/rest/cars/findAll?make=jeep&model=wrangler. The server would search the database for all cars that are Jeep Wranglers then return the results formatted as XML. Your app would download that XML then parse it.

Check out the SeismicXML sample project. NSURLConnection is what connects to your REST API and downloads the data (XML). NSXMLParser can parse the XML you download.

baalexander
+1  A: 

A really cool technique I found for making web services that will only serve iPhones is to find a plist library for your framework. I usually use Django, and Python has a built-in plist library. This means you can turn your data into a plist object, rather than a standard XML file. This can be serialized and sent over the network just like a standard XML file, but you won't need to use NSXMLParser to parse the file.

From your app, you can make an API call by using the NSData object's method:

+ (id)dataWithContentsOfURL:(NSURL *)aURL

This method will create a data object with the contents of the URL, where the url specified is the address of your api. You can then turn that data object into a string, a dictionary, or whatever your root object in the plist file is.

In order to manage the updating, I usually use a waterlevel method. The iPhone stores the time that it last updated in something simple like NSUserDefaults, and the server stores the time of each entry or update in the database. On the api call, you should pass the server the last updated time as a GET or POST parameter. The server will then search it's database for things updated or entered since then, and send only these objects to the phone.

Dan Lorenc