0

I want to paginate data. What is the best way to add pagination when scrolling the tableview? I am trying not to showing all the data at once. For example, every time the user hits the bottom of the tableview the next 10 tableview cells load in (10, 20, 30 and so on.) I am using Swift and Firestore. Here is my code. thanks

     import UIKit
     import FirebaseCore
     import FirebaseFirestore
     import FirebaseAuth

     class MainTableViewController: UIViewController {

     var itemArray:[Item] = []
     var item:Item!


     override func viewDidLoad() {
    super.viewDidLoad()
    
    
   loadListingItems()
  
    
    }


func ListingdownloadITemsFromFirebase(completion: @escaping (_ 
 itemArray: [Item]) -> Void) {
    
    var itemArray: [Item] = []
    
  
   FirebaseReference(.Items).whereField(KISCAR, isEqualTo: true) 
    .order(by: kDATEPOSTED , descending: true).limit(to: 10
    ).getDocuments { (snapshot, error) in
        
        guard let snapshot = snapshot else {
            completion(itemArray)
            return
        }
        
        if !snapshot.isEmpty {
            
            for itemDict in snapshot.documents {
                
                itemArray.append(Item(_dictionary: 
           itemDict.data() as NSDictionary))
            }
        }
        
        completion(itemArray)
    }
}

    private func loadListingItems() {
    
    ListingdownloadITemsFromFirebase { (allITems) in
        print("we have ",allITems.count)
        self.itemArray = allITems
        self.tableView1.reloadData()
    }
}

     func tableView(_ tableView: UITableView, 
      numberOfRowsInSection section: Int) -> Int {
    
        return itemArray.count
        
    }
    

   func tableView(_ tableView: UITableView, cellForRowAt 
   indexPath: IndexPath) -> UITableViewCell {

   cell.generateCell(itemArray[indexPath.row], indexPath: 
   indexPath)

}

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3700449
  • 91
  • 2
  • 10
  • https://firebase.google.com/docs/firestore/query-data/query-cursors? This is probably also good to check out: https://stackoverflow.com/search?tab=votes&q=%5bgoogle-cloud-firestore%5d%20pagination – Frank van Puffelen Oct 09 '21 at 16:46
  • you should cleanup your syntax. Your tabbing is all over the place making it hard to read. The usual way to handle this is to trigger a fetch when you a certain cell appears. For example the 10th last cell (ie a buffer of 10). There is a UITableViewDelegate for cell appearance. You just have to take care to make sure you don't fetch the next page multiple times (as the method can trigger multiple times for the same cell) and your buffer is large enough that the user doesn't see the next page loading or you show some loading indicator at bottom of your table view. – Jacob Oct 09 '21 at 17:05
  • https://stackoverflow.com/questions/47220268/firebase-firestore-pagination-with-swift/64973248#64973248 – trndjc Oct 11 '21 at 15:11

1 Answers1

0

Firestore provides pagination with query cursors which can be used to achieve pagination in the way you are looking for. This document explains how to use pagination in Firestore in detail.

The way you want your data to be fetched it will be good to implement a logic to re-run the query each time the user hits the bottom of the tableview with an increased limit (10,20,30 and so on) which will fetch an increased number of records progressively. I will recommend you to go through this video to know more about it and find out which type of pagination will work for you.

I would suggest you refer to this similar StackOverFlow thread which will help you.

Prabir
  • 1,415
  • 4
  • 10