Pagination with Firebase firestore - swift 4

前端 未结 4 950
情书的邮戳
情书的邮戳 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:51

    My solution was similar to @yambo, however, I tried to avoid making extra calls to the database. After the first call to the database, I get back 10 objects and when it is time to load the new page I kept a reference of how many objects and I checked if the count + 9 is in the range of my new count.

        @objc func LoadMore() {
        let oldCount = self.uploads.count
        guard shouldLoadMore else { return }
        self.db.getNextPage { (result) in
            switch result {
            case .failure(let err):
                print(err)
            case .success(let newPosts):
                self.uploads.insert(contentsOf: newPosts, at: self.uploads.count)
                if oldCount...oldCount+9 ~= self.uploads.count {
                    self.shouldLoadMore = false
                }
                DispatchQueue.main.async {
                    self.uploadsView.collectionView.reloadData()
                }
            }
        }
    }
    

提交回复
热议问题