Search code examples
swiftfirebasegoogle-cloud-firestorecollectionscollectionview

Pagination Firestor CollectionView Swift Programmatically


Regarding Pagination with Firestore I followed the guide on their website and on Youtube but I don't understand one thing:

Practically speaking, if we have a collectionView displaying cells from a first query of 10 documents. We scroll to the bottom of the collectionView when all the data of the first query have been visible, where do we call the next query from the controller?

I searched online but didn't find much, I was thinking maybe with some delegate methods of UIScrollViewDelegate of the CollectionView (?) but It doesn't seem to offer the right methods to do that.

Also for better user experience I was thinking that is better to call the next query when the user is approaching the bottom of the collection and not when he already reached it.


Solution

  •  func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if newInstrumentList.count-1 == indexPath.row && isMoreDataAvailable{
            self.presenter?.showMorePage()
        }
    }
    

    This is what is use for pagination, generally during an API call I get the total number of item and depending on that I set a bool property. Whenever the collection view cell index matches with the indexPath.row, next data is requested.