Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How expensive is UITableView's reloadData?

I'm curious just how expensive in as far as resources go is UITableView's reloadData? I have an app which will make roughly 10 subsequent HTTP requests, and as it gets data / preps, it reloads the tableView. As the data set grows larger and larger, it's becoming very sluggish. I'm trying to figure out if it's because of the amount of times I'm reloading the tableView or because of how I'm grabbing/parsing the data.

What's the best practice in this case?

like image 832
Coocoo4Cocoa Avatar asked Feb 27 '09 16:02

Coocoo4Cocoa


3 Answers

From UITableView.h:

 - (void)reloadData;                 // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks

"This is cheap."

implement your table view methods well and it'll be no big deal to call this function all the time.

On a side note, you should try to use the appropriate methods to animate adding and removing rows if you are thinking of using reloadData for that.

like image 164
Alex Gosselin Avatar answered Oct 05 '22 00:10

Alex Gosselin


The best practice is to have your implementation of cellForRowAtIndexPath: do as little work as possible. In fact, it really shouldn't be doing any work except populating the UITableViewCell instance with the data it needs to display.

You should be using cached UITableViewCells so you don't have to allocate a new cell each time. If you can do your parsing and such in a separate thread and make the parsed data, ready to present, accessible to cellForRowAtIndexPath:, you shouldn't have any performance problems.

You didn't say if you were using a custom UITableViewCell subclass, but if you are, deep view hierarchies can also present a performance problem, since each view in the hierarchy gets drawn. The flatter you can make UITableViewCells, the better.

Hope that gets you moving in the right direction.

like image 36
Alex Avatar answered Oct 05 '22 02:10

Alex


Best thing to do is profile your app to see where it is slow.

That said, if your table cells are all the same height, then I think

reloadData

only has to call

cellForRowAtIndexPath

for cells that are visible on screen.

like image 42
Chris Lundie Avatar answered Oct 05 '22 00:10

Chris Lundie