【问题标题】:Paginate a Firebase query by timestamp按时间戳对 Firebase 查询进行分页
【发布时间】:2018-03-16 07:50:01
【问题描述】:

假设我要检索具有以下结构、分页、按时间戳排序的帖子:

post_key: {
    message: "message"
    timestamp: 1234567890
}

我可以使用.queryOrdered(byChild: "timestamp").queryLimited(toLast: 5)... 进行初始提取。但是对于第二次提取,如果上一次提取的最后一个时间戳有重复,则会出现问题。

我必须以某种方式通过timestamp 订购并以一个键结束。比如:

.queryOrdered(byChild: "timestamp").queryEnding(atPOSTkey: lastPostKey)

任何想法如何解决这个问题?我想到的一种方法是将时间戳与 post 键连接起来,但我认为有一个更简单的解决方案(我看到有函数 queryEnding(atValue, childKey) 但似乎并不能真正使它工作)。


编辑 1:

我想我设法使它工作如下:ref.child("posts/byLocation/\(userLocation)/\(dashedInterests)").queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp, childKey: lastPostKey).queryLimited(toLast: postsPerPage + 1).observeSingleEvent(of: .value) ...

问题是现在我总是收到这个警告:

使用未指定的索引。您的数据将被下载和过滤 在客户端。考虑添加 ".indexOn": "timestamp" 在 /posts/byLocation/1/1-2-3 到您的安全规则以获得更好的 性能。

我无法真正添加 .indexOn 规则,因为我有几个动态生成的节点,例如 1-2-3(例如 1、2、3、1-2、1-3 等)


编辑 2:

我添加了以下规则:

"posts": {
        "$dashed_interests": {
            ".indexOn": "timestamp"
        },
      "byLocation": {
        "1": {
          "$dashed_interests": {
            ".indexOn": "timestamp"
            } 
      }
}

我不再收到警告。

如果有人能确认我做的很好,那就太好了!

谢谢。

【问题讨论】:

    标签: swift firebase firebase-realtime-database


    【解决方案1】:

    我设法使其工作如下:ref.child("posts/byLocation/\(userLocation)/\(dashedInterests)").queryOrdered(byChild: "timestamp").queryEnding(atValue: lastTimestamp, childKey: lastPostKey).queryLimited(toLast: postsPerPage + 1).observeSingleEvent(of: .value) ... 其中 lastPostKey 和 lastTimestamp 是上次提取的最后一个键和时间戳。

    还有一个问题,因为我总是收到这个警告:

    使用未指定的索引。您的数据将被下载和过滤 在客户端。考虑添加 ".indexOn": "timestamp" 在 /posts/byLocation/1/1-2-3 到您的安全规则以获得更好的 性能。

    由于我有几个动态生成的节点,例如 1-2-3(例如 1、2、3、1-2、1-3 等),我通过添加以下规则来解决它:

    "posts":{
            "byLocation":{
               "$location":{
                  "$dashed_interests":{
                     ".indexOn":"timestamp"
                  }
               }
            }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 2019-02-08
      • 1970-01-01
      • 2018-10-19
      • 2015-02-12
      • 2020-12-20
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多