我在Firebase实时数据库中有62条记录。我想一次使用表格视图(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()
}
}
}
}
在下面的图片中显示了一些记录,该表有20条记录,这些记录以-LhZC4FWPCaOGtq03iho开头,以-LhZCF41n7Xh8g57YN29结尾。当用户向下滚动时,我需要显示以LhZCF41n7Xh8g57YN2B开头的下20条记录。
那我做错了什么?我可以在这里和那里将queryStarting
更改为queryEnding
或将queryLimited(toFirst: xxx)
更改为queryLimited(toLast: xxx)
,以便使该表加载另外20条记录,只是该表最终会显示准确的同一组记录。谢谢。
答案 0 :(得分:3)
我使用下面的代码实现了相同的目的。您可以使用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年做到了。
谢谢