views:

626

answers:

1

Hi All,

I am navigating from tableview1.row to a tableview2 which has a LOT of rows being fetched. Given the load time is around 3 seconds, I want the navigation to slide into tableview2 as soon as the tableview1.row is selected, and then display a UIActivityIndicatorView above tableview2 whilst the data is fetched and then rendered in its underlying table view. Note, tableview2 is actually a subview of the parent UIView (as opposed to the parent being a UITableView).

I've seen this post: http://stackoverflow.com/questions/2153653/activity-indicator-shold-be-displayed-when-navigating-from-uitableview1-to-uitabl

... which gives instructions to add the activity indicator start and stopAnimating calls around the data fetch into viewDidLoad of tableview2.

Thing is, I'm not sure how the above solution could work as viewDidLoad runs and completes before tableview2 visibly slides into view.

Separately, I also tried adding an activity indicator over tableview2 in IB and added the IBOutlet indicator's start/stop animating code into viewDidAppear. What happens is the data fetch runs and I can see the indicator spinning but at the end of the fetch, the table view is empty. Seems like viewDidAppear is too late to add data to the table view as cellForRowAtIndexPath etc has already fired.

Can anyone please suggest any pointers? I could very well be missing something obvious here (its nearly 5am where I am and think my brain is mush). Should I re-trigger cellForRowAtIndexPath etc from viewDidAppear? Is the issue that my table view is a subview and not the parent view?

Thanks

A: 

I took another look at my own question and the answer is actually pretty straight forward. Any very intensive processing should generally not be run on the main thread. I was attempting to do my data intensive fetch AND UI display of the activity indicator on the same thread and so these were being processed in sequence, one after the other.

Solution is to enter my new view controller and a) kick off the activity indicator animation on the main thread and then b) run the data intensive fetch on another thread (using performSelectorInBackground). Any UI related updates cannot be applied from the background thread. These should be handed back to the main thread.

Plenty of SO posts on this already (example here), my bad for not picking up on these from the start :)

Tofrizer