I am writing an app that uses multithreading and cache. Quite similar to Apple’s TopSongs sample code. Upon startup, I need to grab the value of an object in a JSON feed.
Now I am afraid doing so would ruin the workflow of the app, and block a thread or something if I use NSURLConnection. Can I just download that JSON feed (it only has one object) without using NSURLConnection’s delegate methods? If I implement the delegate methods in my delegate file, then the app will finish up applicationDidFinishLaunching:application method and THEN go to
connection:didReceiveResponse:
connection:didReceiveData:
connection:didFailWithError:
connectionDidFinishLoading:
How can I avoid that? I need to get the timestamp (stored in that JSON file) right at the beginning of applicationDidFinishLaunching:application as the rest of that method depend on that timestamp.
Thank you,
There are two ways to achieve this:
Use the sendSynchronousRequest of NSURLConnection, but this will block until a response is received
or (the preferable option)
Move the code which relies on the JSON to a seperate method. Use the asynchronous + delegate methods of NSURLConnection as normal, but in your applicationDidFinishLaunching method display some sort of HUD to inform the user of what’s happening and then in the connection:didReceiveResponse method or connectionDidFinishLoading method call your new method which relies on the JSON (and will now have the JSON) and dismiss the HUD.