swiftiglistkit

Lag issue while scrolling vertically using nested adapter | IGListKit


I'm using IGListKit to show images vertically and horizontally. That's why I used nested adapter approach to have horizontally scrolling collectionViews inside a vertically scrolling collectionView. But when having many images/items it starts to lag every time a new row is being displayed. The scrolling stops for a few milliseconds and then goes on.
I have set a level and button in vertical collection views (first adapter). Inside every vertical Collection view, I have created another adapter to show images.
Help me to solve this lagging issue.
Full project link: https://github.com/sagarthecoder/IGListKit-Dummy-Project
This is my partial code from ViewController.swift file

class ViewController: UIViewController {
   
    @IBOutlet weak var collectionView: UICollectionView!
    lazy var adapter : ListAdapter = {
        return ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 0)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        adapter.collectionView = collectionView
        adapter.dataSource = self
    }
}

extension ViewController : ListAdapterDataSource {
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        return someItems;   
    }
    
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        return RootSectionController()  
    }
}

This is my partial code from RootSectionController.swift file where I add another adapter

class RootSectionController: ListSectionController{
    
    var header : Header! // Items that came from ViewController
    lazy var adapter: ListAdapter = {
          let adapter = ListAdapter(updater: ListAdapterUpdater(),
                                    viewController: self.viewController)
          adapter.dataSource = self
          return adapter
      }()
}

extension RootSectionController {

    override func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell : RootCollectionViewCell = (collectionContext?.dequeueReusableCell(withNibName: "RootCollectionViewCell", bundle: nil, for: self, at: index)) as! RootCollectionViewCell
        
        cell.categoryName.text = header.categoryName
        adapter.collectionView = cell.collectionView
        return cell
        
    }
    override func didUpdate(to object: Any) {
        header = object as? Header
    }
}

extension RootSectionController : ListAdapterDataSource {
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        return someItems 
    }
    
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        return UserSectionController()
    }
}

This is my partial code from User.Swift file

class User: NSObject {
    let id : Int
    let name: String
    let imageName : String
}
extension User: ListDiffable {

    func diffIdentifier() -> NSObjectProtocol {
        return id as NSObjectProtocol
    }

    func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
        if (object as? User) != nil  {
            return true
        } else {
            return false
        }
    }
}

This is my Header.swift file

class Header: NSObject {
    let categoryName : String
}

extension Header : ListDiffable {
    func diffIdentifier() -> NSObjectProtocol {
        return categoryName as NSObjectProtocol
    }
    
    func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
       
        if let object = object as? Header {
            return categoryName == object.categoryName
        } else {
            return false
        }
    }
}

Solution

  • Make sure you are not doing any unnecessary operations (like printing or anything else). Sometimes printing many can create lag issues during debugging. IGListKit manages your data and it is not supposed to lag your UI because of IGListKit.