Swift iOS:Firebase分页

前端之家收集整理的这篇文章主要介绍了Swift iOS:Firebase分页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个Firebase数据:

我想通过分页查询帖子数据.目前我的代码正在将此JS代码转换为Swift代码

  1. let postsRef = self.rootDatabaseReference.child("development/posts")
  2. postsRef.queryOrderedByChild("createdAt").queryStartingAtValue((page - 1) * count).queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in
  3. ....
  4.  
  5. })

访问时,此数据页: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(_ :)附加查询.

样品用法

  1. var count = numberOfItemsPerPage
  2.  
  3. var query ref.queryOrderedByKey()
  4.  
  5. if startKey != nil {
  6. query = query.queryStartingAtValue(startKey)
  7. count += 1
  8. }
  9.  
  10. query.queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in
  11. guard var children = snapshot.children.allObjects as? [FIRDataSnapshot] else {
  12. // Handle error
  13. return
  14. }
  15.  
  16. if startKey != nil && !children.isEmpty {
  17. children.removeFirst()
  18. }
  19.  
  20. // Do something with children
  21. })

猜你在找的Swift相关文章