如何在swift中添加分页?

如何在swift中添加分页?,swift,google-cloud-firestore,Swift,Google Cloud Firestore,我花了这么多时间从Firebase的文档中找到了一个解决方案,但没有成功。我使用Swift 5.3和Firestore,并具有以下代码: func readFlights() { Spinner.startAnimating() let myquery = db.collection("flight").limit(to: 25).whereField("Userid", isEqualTo: userID).order(

我花了这么多时间从Firebase的文档中找到了一个解决方案,但没有成功。我使用Swift 5.3和Firestore,并具有以下代码:

func readFlights() {
        Spinner.startAnimating()
         let myquery = db.collection("flight").limit(to: 25).whereField("Userid", isEqualTo: userID).order(by: "DateDB", descending: true)
            .order(by: "Start", descending: true)
         myquery.getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in querySnapshot!.documents {
                    self.prefixArray.append(document.get("Prefix") as! String)
                    self.dateArray.append(document.get("Date") as! String)
                    self.startArray.append(document.get("Start") as! String)
                    self.stopArray.append(document.get("Stop") as! String)
                    self.landingArray.append(document.get("Landing") as! String)
                    self.takeOffArray.append(document.get("Takeoff") as! String)
                    self.flightTimeArray.append(document.get("FlightTime") as! String)
                    self.engineTimeArray.append(document.get("EngineTime") as! String)
                    self.idArray.append(document.get("id") as! String)
                    self.destinationArray.append(document.get("Destination") as! String)
                    self.originArray.append(document.get("Origin") as! String)
                    self.informationArray.append(document.get("Addinfo") as! String)
                    self.rulesArray.append(document.get("VFRIFR") as! Int)
                    self.pilotCopilotArray.append(document.get("PilotoCopiloto") as! Int)
                    self.engineArray.append(document.get("MnteMlte") as! Int)
                    self.dayNightArray.append(document.get("DayNight") as! Int)
                }
                DispatchQueue.main.async{
                self.tabelView.reloadData()
                self.Spinner.stopAnimating()
                }
            }
        }
工作正常,但我需要包括在这个代码分页。这意味着当我从Firestore收到前25条记录时,我用手指滑入列表,所以我希望在最新记录之后,他加载更多的25条记录并显示它们


谢谢你的帮助。谢谢

通过使用UITableViewDelegate,您可以调用该函数。每次滚动到底部时,它都会检查最大限制,如果条件为真,则再次获取数据

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let lastItem = self.array.count - 1
    if indexPath.row == lastItem {
        if limit < max_limit {
            limit += 25
           //Get data from Server
           readFlights(limit:Int)
        }
    }
}
func tableView(tableView:UITableView,willDisplay单元格:UITableView单元格,forRowAt indexath:indexPath){
让lastItem=self.array.count-1
如果indexPath.row==lastItem{
如果极限<最大极限{
上限+=25
//从服务器获取数据
读取航班(限制:整数)
}
}
}

max_limit是指限制的总数,通常,它由服务器以meta形式返回,您可以从以下位置使用awesome解决方案:

例如:

tableView.addInfiniteScroll { (tableView) -> Void in
    readFlights("offset if need")
    tableView.finishInfiniteScroll()
}

首先,创建作为视图/视图控制器实例属性的文档游标:

var cursor: DocumentSnapshot?
let pageSize = 25 // for convenience
其次,将页面大小应用于查询:

let myquery = db.collection("flight").limit(to: pageSize).whereField("Userid", isEqualTo: userID).order(by: "DateDB", descending: true).order(by: "Start", descending: true)
第三,无论何时从Firestore收到快照,都要在返回的某个点更新光标(理想情况下,在打开快照之后和解析文档之前):

func getData(){
getDocuments(完成:{(快照,错误)在
...
如果snapshot.count<页面大小{
/*因此,这份申报表只有不到25份文件
没有更多可能的文档可供获取和保存
因此没有光标*/
self.cursor=nil
}否则{
/*因此,这份申报表至少有25份文件
可能有更多的文档要获取,这会使
此快照中的最后一个文档是游标*/
self.cursor=snapshot.documents.last
}
...
})
}
最后,每当用户滚动到底部时,使用光标获取另一页:

func continueData() {
    guard let cursor = cursor else {
        return // no cursor, exit
    }
    myquery.start(afterDocument: cursor).getDocuments(completion: { (snapshot, error) in
        ...
        // always update the cursor whenever Firestore returns
        if snapshot.count < self.pageSize {
            self.cursor = nil
        } else {
            self.cursor = snapshot.documents.last
        }
        ...
    })
}
func continueData(){
guard let cursor=cursor else{
返回//无光标,退出
}
myquery.start(afterDocument:cursor).getDocuments(完成:{(快照,错误)在
...
//始终在Firestore返回时更新光标
如果snapshot.count

对于流体用户体验,您将需要大大细化该代码,但这是您可以对FixSturo进行分页的基础。您也可以在Firestore中使用文档偏移量(而不是文档光标)分页,但这是要避免的(有关原因,请参阅文档)

func continueData() {
    guard let cursor = cursor else {
        return // no cursor, exit
    }
    myquery.start(afterDocument: cursor).getDocuments(completion: { (snapshot, error) in
        ...
        // always update the cursor whenever Firestore returns
        if snapshot.count < self.pageSize {
            self.cursor = nil
        } else {
            self.cursor = snapshot.documents.last
        }
        ...
    })
}