【发布时间】: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 更改为queryEnding 或queryLimited(toFirst: xxx) 到queryLimited(toLast: xxx),以便让表加载另外20 条记录,但表最终将显示完全相同的记录集。谢谢。
【问题讨论】:
标签: ios swift firebase firebase-realtime-database pagination