【问题标题】:Firebase Realtime Database Pagination, Showing 20 Records at a TimeFirebase 实时数据库分页,一次显示 20 条记录
【发布时间】:2019-10-31 13:59:52
【问题描述】:

我在 Firebase 实时数据库中有 62 条记录。我想用表格视图一次显示 20 条记录 (UITableView)。我可以毫无问题地显示前 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()
            }
        }
    }
}

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

那我做错了什么?我可以在这里和那里将queryStarting 更改为queryEndingqueryLimited(toFirst: xxx)queryLimited(toLast: xxx),以便让表加载另外20 条记录,但表最终将显示完全相同的记录集。谢谢。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database pagination


    【解决方案1】:

    我使用下面的代码也达到了同样的效果。您可以使用 queryOrderedByKey 进行排序。

    代码:

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

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

    谢谢

    【讨论】:

    • 谢谢。如果我将您的代码用于 readMoreData(),则应用程序将读取与 readData() 完全相同的记录集。
    • 如果 queryEnding(atValue:) 请使用 queryStarting 代替,因为它会返回与我们的要求相反的结果。
    • 嗯...非常接近。如果我将查询设置为 let query = refInventory.queryOrderedByKey().queryStarting(atValue: queryKey).queryLimited(toFirst: 20),readMoreData() 将从第 20 条记录开始,而不是从第 21 条记录开始。
    • 发生这种情况是因为我们提供了第 20 个记录键作为起点。如果您要提供第 21 个记录密钥,那么一切都会按预期进行。
    • 谢谢。虽然我不太喜欢我正在做的事情,但这很有效。我希望 Firebase 实时数据库也能支持像 Cloud Firestore 这样的光标。
    猜你喜欢
    • 2020-04-09
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多