Firebase 实时数据库分页,一次显示 20 条记录

El *_*ato 1 pagination ios firebase swift firebase-realtime-database

我有 62 条 Firebase 实时数据库记录。我想使用表视图 ( UITableView)一次显示 20 条记录。我可以毫无问题地显示前 20 条记录。但是当用户向下滚动到底部时,表格不会显示下一组。

import UIKit
import Firebase

class ReadViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var refInventory: DatabaseReference!
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        refInventory = Database.database().reference().child("inventory")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        readData()
    }

    var dataObject = [DataObject]() // DataObject is an `NSObject` object with three properties (id, uuid, number).
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataObject.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
        let object = dataObject[indexPath.row]
        cell.uuidField.text = object.uuid
        cell.numberField.text = String(object.number)
        return cell
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row >= dataObject.count - 1 {
            readMoreData()
        }
    }

    func readData() {
        let query = refInventory.queryLimited(toFirst: 20)
        query.observe(DataEventType.value) { (snapshot) in
            guard let first = snapshot.children.allObjects.first as? DataSnapshot else {
                return
            }
            guard let last = snapshot.children.allObjects.last as? DataSnapshot else {
                return
            }
            print(first.key);print(last.key)
            if snapshot.childrenCount > 0 {
                for items in snapshot.children.allObjects as! [DataSnapshot] {
                    let object = items.value as! [String: AnyObject]
                    let uuid = object["uuid"] as! String
                    let num = object["number"] as! Int
                    let myObject = DataObject()
                    myObject.uuid = uuid
                    myObject.number = num
                    self.dataObject.append(myObject)
                }
                self.queryKey = last.key // -LhZCF41n7Xh8g57YN29
                self.tableView.reloadData()
            }
        }
    }

    var queryKey = String()
    func readMoreData() {
        let query = refInventory.queryLimited(toFirst: 20).queryStarting(atValue: queryKey)
        query.observe(DataEventType.value) { (snapshot) in
            // It stops right here //
            guard let first = snapshot.children.allObjects.first as? DataSnapshot else {
                return
            }
            guard let last = snapshot.children.allObjects.last as? DataSnapshot else {
                return
            }
            print(first.key);print(last.key)
            if snapshot.childrenCount > 0 {
                for items in snapshot.children.allObjects as! [DataSnapshot] {
                    let object = items.value as! [String: AnyObject]
                    let uuid = object["uuid"] as! String
                    let num = object["number"] as! Int
                    let myObject = DataObject()
                    myObject.uuid = uuid
                    myObject.number = num
                    self.dataObject.append(myObject)
                }
                self.queryKey = last.key
                self.tableView.reloadData()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下图显示了一些记录,该表有 20 条记录,以 -LhZC4FWPCaOGtq03iho 开头,以 -LhZCF41n7Xh8g57YN29 结尾。当用户向下滚动时,我需要显示接下来的 20 条以 LhZCF41n7Xh8g57YN2B 开头的记录。

在此处输入图片说明

那么我做错了什么?我可以改变queryStartingqueryEndingqueryLimited(toFirst: xxx)queryLimited(toLast: xxx)这里和那里为了得到表加载除了表将最终呈现完全相同的一组记录的另外20条记录。谢谢。

Hit*_*esh 5

我使用以下代码实现了相同的目标。您可以使用 queryOrderedByKey 进行排序。

代码:

ref.queryOrderedByKey().queryStarting(atValue: self.queryKey).queryLimited(toLast: 20).observeSingleEvent(of: .value, with: { snapshot in            
    // Do stuff with this page of elements                                                                                                                              
})
Run Code Online (Sandbox Code Playgroud)

由于最新版本的 firebase SDK,某些方法可能已被弃用或不可用。我在 2017 年做到了。

谢谢