1

I'm out in the woods with this one: I have a universal, navigation-based app that displays data currently stored in a plist file. In a future release, I want to migrate the database to a JSON file on my server which the app can download to it's bundle, then parse it. Can anyone suggest a simple light-weight way of checking that the currently stored file in the bundle matches the version hosted on the server? Essentially checking for updates to the db without re-downloading the entire JSON file.

Here's a snippet of what the beginning of the JSON file currently looks like.

{ "version" : "0.2", "description" : "1. Corrections to several entries.\n2. Added 21 new departments from Alameda & Fresno Counties.", "counties" : {............... *Rest of the JSON file here* .....}

My idea was to store the "version" ("0.2") value to NSUserDefaults and use that value to check against the available JSON file online every time the app launches.

Am I on the right track or is there a better way of doing this altogether?

Thank you

Romeo

2 Answers 2

3

You can add the If-Modified-Since header to an instance of NSMutableURLRequest. If the document on the server has changed since that date, you'll get the data back. If it hasn't changed, you get a 304 Not Modified and no data.

This is much better than making a HEAD request because in the event of an updated file, you're only making one request instead of two.

4
  • Pardon my noobness, I'm looking over the NSMutableURLRequest Class Reference, am I using - addValue:forHTTPHeaderField: or - setValue:forHTTPHeaderField:?
    – Romeo
    Jul 27, 2012 at 3:19
  • Like this? [myRequest setValue:@"Fri, 20 Jul 2012 04:17:16 GMT" forHTTPHeaderField:@"If-Modified-Since"];
    – Romeo
    Jul 27, 2012 at 3:33
  • When I print out the allHeaderFields dictionary from the NSHTTPURLResponse object, sometimes I get a Last-Modified key and sometimes I don't. I'm trying to store this value into NSUserDefaults to pass it into the If-Modified_Since header in future launches of the app. Any suggestions?
    – Romeo
    Jul 27, 2012 at 19:47
  • Okay, I think I figured it out. Reading over the Header Field Definitions, I see that I was wrong in checking for the If-Last-Modified key in the dictionary, instead of looking for status code 304. Sometimes typing or saying something out loud helps you clear up the problem.
    – Romeo
    Jul 27, 2012 at 20:16
1

Do a HEAD request (instead of a GET) and check the LastModified header. if the file has been modified since the last time you checked, download the file. Save the modified time somewhere to compare against next time.

You can set the http method on the request object like so:

 [request setHTTPMethod:@"HEAD"];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.