views:

288

answers:

3

I am trying to load a table view from a cache very quickly and have the cached data in the table view appear. Then I want download new data, and then reload the table. Right now I am downloading the new data on viewDidAppear, but the view still refreshes before it displays. Any idea how I can do this?

A: 

Simply calling [tableView reloadData] after the download might do the trick. This will trigger a refresh of your table cells.

To download the new data, you may consider using Cocoa Streams, particular an asynchronous Socket Stream. In the stream delegate, call reloadData when the download is completed.

Martin Cote
I know how to do that, but I need to actually re-download the data from the web. I'm thinking @paull is on the right track, but I still don't know how to do that in the delegate.
Alavoil
A: 

viewDidAppear is not a good place to download data; it is really intended for clean up after presenting data, so I can understand why you used it. You should request your data reload as early as possible, such as in viewDidLoad or viewWillAppear (depending on your reuse or otherwise of view controllers).

If you are doing asynchronous downloads, which you should be, put the reloadData call in your delegate callback function for when the data is completed.

Paul Lynch
What would the code be in the delegate for this?
Alavoil
That's hard to say, because you didn't say how you are doing the download. But it should be using NSURLConnection. The code to do the refresh of the table is exactly as posted.
Paul Lynch
I know how to pull the data from the web (NSURLConnection), and I know how to reload the table with new or existing data ([tableView reloadData]). My question is how do I get it do do it in the background after launch, and after displaying my cached data from the previous launch. I'm not sure how to put those pieces into the delegate to download asynchronously.
Alavoil
A: 

I ended up implementing the delegate class to do this asynchronously. This example was extremely helpful, and I implemented much of its code:

http://developer.apple.com/iphone/library/samplecode/URLCache/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008061-Intro-DontLinkElementID_2

Alavoil