Swift iOS:Firebase分页

我有这个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
})

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...