Swift 快速firebase分页查询器

Swift 快速firebase分页查询器,swift,firebase,firebase-realtime-database,pagination,Swift,Firebase,Firebase Realtime Database,Pagination,我正在对来自firebase的数组进行分页,我可以在开始时对数组进行索引,但分页后我无法获得正确的顺序,我得到: 我在表视图中显示的邮政订单如下 13(最新帖子)-12-11-10-9-!!6-7-8-4-5-6-1-2-3 我需要: 13…1 我的代码: var startKey: String! func handlePagination() { let ref = Api.userApi.DB_REF.child("Posts").queryOrderedBy

我正在对来自firebase的数组进行分页,我可以在开始时对数组进行索引,但分页后我无法获得正确的顺序,我得到:

我在表视图中显示的邮政订单如下 13(最新帖子)-12-11-10-9-!!6-7-8-4-5-6-1-2-3

我需要: 13…1

我的代码:

var startKey: String!

func handlePagination() {
    let ref = Api.userApi.DB_REF.child("Posts").queryOrderedByKey()
    if startKey == nil {
        ref.queryLimited(toLast: 5).observeSingleEvent(of: .value) { (snapshot) in
            
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else {return}
            
            if snapshot.childrenCount > 0 {
                
                for child in snapshot.children.allObjects as! [DataSnapshot]{
                    guard let dictionary = child.value as? [String:Any] else {return}
                    self.posts.insert(PostR(dictionary: dictionary), at: 0)
                }
                self.startKey = children.key
                self.tableView.reloadData()
            }
        }
    } else {
        ref.queryEnding(atValue: startKey).queryLimited(toLast: 4).observeSingleEvent(of: .value) { (snapshot) in
            
            guard let children = snapshot.children.allObjects.first as? DataSnapshot else {return}
            if snapshot.childrenCount > 0 {

                for child in snapshot.children.allObjects as! [DataSnapshot] {

                    if child.key != self.startKey {

                        guard let dictionary = child.value as? [String:Any] else {return}
                        self.posts.insert(PostR(dictionary: dictionary), at: self.posts.count)
                    }
                }
                self.startKey = children.key
                self.tableView.reloadData()
            }
        }
    }
}
谢谢