Search code examples
iosswiftscrolluicollectionviewreloaddata

Not scrolling smooth on UICollectionViewController for large data loading


I am new to iOS and I'm trying to load data in collectionviewcontroller. Data is loading and scrolling for less data. But scroll get stuck when loading large data. But as data in my collection gets increasing, its scrolling becomes rough and after loading some pages, it becomes very hard to scroll up or down. It is getting stuck.


Solution

  • Do not reload collectionView in cellForItem, delete this part:

        DispatchQueue.main.async {
    
            self.collectionView?.reloadData()
    
        }
    

    And delete most of those reloadData calls that you have there. It's a heavyweight operation - call it only when you really need to reload it - meaning when the data model changes for the whole collection view.

    Sidenote:

    Don't use indexPath.row, but rather indexPath.item, row is used for UITableViews, not for UICollectionViews.