【问题标题】:Firebase QueryOrderedByChild along with EqualToValue and StartingAtValue CombinationFirebase QueryOrderedByChild 以及 EqualToValue 和 StartingAtValue 组合
【发布时间】:2016-08-13 23:12:42
【问题描述】:

我正在尝试通过等于某个值的子级查询 Firebase,但在特定的发布键处启动快照。

数据结构:

projectName
  -posts
    -postKey1
      -storeId: 1
    -postKey2
       -storeId: 2
    -postKey3
       -storeId:3
    -postKey4
       -storeId:2
    -postKey5
       -storeId:3
    -postKey6
       -storeId:2

示例:

我正在尝试 queryOrderedByChild("storeId")queryEqualToValue("2") 但我希望快照在按顺序返回时从“postKey4”开始。

我当前的查询:

ref.queryOrderedByChild("storeId").queryEqualToValue("\(myId)").observeEventType(.Value, withBlock: {snapshot in 

我知道您不能在之前调用 queryStartingAtValuequeryEndingAtValuequeryEqualToValue 之后调用 queryEqualToValue:,但我需要 queryEqualToValue 来获取我的 storeId。非常感谢任何帮助!

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    您可以将第二个参数传递给queryStartingAtValue,指定开始的键。

    ref.queryOrderedByChild("storeId")
       .queryStartingAtValue(2, childKey: "postKey4")
       .queryEndingAtValue(2)
       .observeEventType(.Value, withBlock: {snapshot in
           for childSnapshot in snapshot.children {
               print(childSnapshot.key!!
           }
    })
    

    打印出来:

    postKey4

    postKey6

    我必须承认这是我第一次使用queryStartingAtValue 的第二个参数,所以我可能还没有完全理解。

    你不能使用queryEqualToValue的原因是它只会返回一个键,这不是你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多