viewWillAppear, p. 233 - performance?


#1

Hi

After saving the (potential) changes made in the DetailViewController, we are instructed to implement viewWillAppear in the ItemsViewController to reflect the changes made by the DetailViewController:

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear]; [[self tableView] reloadData]; }

So far so good! But this point is not the only point viewWillAppear will be called, for example it is called when we just instantiated the ItemsViewController and its tableView is about to appear for the first time. It seems to me in such cases reloadData has no effect since there is no way the data could have been changed. And since reloadData is a pretty intense method shouldn’t we be more careful when using it?

Cheers
Simon


#2

viewWillAppear: is sent any time the table view is going to go on the screen. This ensures that the table is always showing the most current data.

Don’t worry about the performance of reloadData. It is very fast. If you see an issue with the application and have profiled it enough to determine that the messages sent during reloadData are the culprit, then the table view data source methods you have implemented are problematic.

edit: Just to clarify, the only way reloadData becomes intense is if the data source methods have been implemented very poorly. For example, performing calculations in the data source methods is bad form. When your model objects change, compute the values for anything that will be displayed in the table and hold onto them, then simply return them in the data source methods.

I can’t say I’ve seen a table view controller that doesn’t reload its table when sent viewWillAppear:.


#3

Only think that I didn’t get it. When we say reloadData, we are not updating the array so how the tableView knows that we changed that specific row?

Cheers


#4

will update the ALL rows of the tableView, not just a single row. The tableView does not need to know which row has been changed when you call reloadData.

will update the ALL rows of the tableView, not just a single row. The tableView does not need to know which row has been changed when you call reloadData.