Pagination with Firebase firestore - swift 4

前端 未结 4 952
情书的邮戳
情书的邮戳 2021-02-03 15:08

I\'m trying to paginate data (infinitely scroll my tableview) using firestore. I\'ve integrated the code google gives for pagination as best I can, but I\'m still having problem

4条回答
  •  日久生厌
    2021-02-03 15:46

    Simple, Fast and easy way is...

    class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FeedCellDelegate {

    private var quotes = [Quote]() {
        
        didSet{ tbl_Feed.reloadData() }
    }
    
    var quote: Quote?
    var fetchCount = 10
    
    @IBOutlet weak var tbl_Feed: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        fetchPost() 
    }
    
    
    // MARK: - API
    
    func fetchPost() {
        
        reference(.Quotes).limit(to: getResultCount).getDocuments { (snapshot, error) in
            
            guard let documents = snapshot?.documents else { return }
            
            documents.forEach { (doc) in
                
                let quotes = documents.map {(Quote(dictionary: $0.data()))}
                
                self.quotes = quotes
            }
        }
    }  
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        let currentOffset = scrollView.contentOffset.y
        let maxxOffset = scrollView.contentSize.height - scrollView.frame.size.height
        
        if maxxOffset - currentOffset <= 300 { // Your cell size 300 is example
    
            fetchCount += 5
            fetchPost()
    
            print("DEBUG: Fetching new Data")
        }
    }
    

    }

提交回复
热议问题