Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is calling UICollectionView.reloadData() bad practice? If so, why?

I worked with a brilliant developer once who more or less refused to ever call reloadData() on a UICollectionView.

He even frowned upon collectionView.reloadItemsAtIndexPaths(collectionView.indexPathsForVisibleItems()), preferring instead to reload only the exact index paths that required a visual update.

I picked this up from him but am struggling to think of what problems specifically would be caused by a generic reloadData(). Of course, if you have expensive side-effects that are triggered by a reload, that's one thing. But if your data source itself is just hanging out, why not reloadData() as an easy-on-the-coding-side way of updating the collection view?

What problems will be caused by this practice?

like image 464
buildsucceeded Avatar asked Feb 18 '16 08:02

buildsucceeded


People also ask

What does Collectionview reloadData do?

reloadData()Reloads all of the data for the collection view.

How does UICollectionView work?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.

What is the difference between Uitableview and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .


1 Answers

No, it is not a bad practice at all.

Just a short overview, So you get your answer

UICollectionView is highly optimized, and thus only keep On-screen visible rows in memory. Now, All rows Cells are cached in Pool and are reused and not regenerated. Whenever, user scrolls the UICollectionView, it adds the just-hidden rows in Pool and reuses them for next to be visible rows.


So, when you call reloadData(), it simply updates the data in row cells, like updating the UILabel text and the update occurs only for visible cells, and the process goes on.

Now, Consider you have ten rows visible in your collection view and while calling

reloadItemsAtIndexPaths(collectionView.indexPathsForVisibleItems())

you simply update the UILabel like elements for only those items, but if you call reloadData(), you update the data for ten visible items, which doesn't make any difference as it is a very light process.

You should use reloadData() everywhere. Keep it simple and readable. Performance wise, reloadItems doesn't make any difference.

It is properly elaborated in Apple Docx

Apple Link

Call this method to reload all of the items in the collection view. This causes the collection view to discard any currently visible items and redisplay them. For efficiency, the collection view only displays those cells and supplementary views that are visible. If the collection data shrinks as a result of the reload, the collection view adjusts its scrolling offsets accordingly.

like image 124
gunjot singh Avatar answered Oct 01 '22 21:10

gunjot singh