我有这个Firebase数据:
我想通过分页查询帖子数据.目前我的代码正在将此JS代码转换为Swift代码
let postsRef = self.rootDatabaseReference.child("development/posts")
postsRef.queryOrderedByChild("createdAt").queryStartingAtValue((page - 1) * count).queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in
....
})
访问时,此数据页:1,计数:1.我可以获取“posts.a”的数据但是当我尝试访问页面时:2,计数:1返回仍然是“posts.a”
我在这里想念的是什么?
假设您在将数据推送到Firebase时正在或将要使用childByAutoId(),您可以使用queryOrderedByKey()按时间顺序排序数据. Doc
here.
The unique key is based on a timestamp,so list items will automatically be ordered chronologically.
要启动特定键,您必须使用queryStartingAtValue(_ :)附加查询.
样品用法:
var count = numberOfItemsPerPage
var query ref.queryOrderedByKey()
if startKey != nil {
query = query.queryStartingAtValue(startKey)
count += 1
}
query.queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in
guard var children = snapshot.children.allObjects as? [FIRDataSnapshot] else {
// Handle error
return
}
if startKey != nil && !children.isEmpty {
children.removeFirst()
}
// Do something with children
})