【问题标题】:Firebase queryEqual(toValue:Firebase queryEqual(toValue:
【发布时间】:2017-06-20 22:14:52
【问题描述】:

我正在尝试快速使用 queryEqual(toValue:。我遇到的问题是我得到了 null。

我的数据是这样的

"time" : {
      "149791341619654" : {
        "URL_ID" : "02D03DA8-A652-4EFA-946F-1F0F2A963449",
        "UUID" : "F496B98E-3B4E-4602-B06D-C09B7F42DCB5",
        "date" : 1.497913416196546E9,
        "huer" : 0,
        "latitude" : 32.87926715336076,
        "longitude" : -117.2139702738423,
        "type_" : "Image",
        "uid" : "BjN8usKaW7NhqD2DxPPqAkjhnSx2"
      },
      "149791381894099" : {
        "URL_ID" : "AB8D20E9-D1EB-4FD0-B0A8-CE755D1D8B91",
        "UUID" : "F534E1FC-DC55-4AFB-847D-64B614EF7137",
        "date" : 1.497913818940991E9,
        "huer" : 0,
        "latitude" : 32.87926715336076,
        "longitude" : -117.2139702738423,
        "type_" : "Image",
        "uid" : "BjN8usKaW7NhqD2DxPPqAkjhnSx2"
      },
      "149791881234864" : {
        "URL_ID" : "A3207072-FC64-4A09-87C0-E1340051B140",
        "UUID" : "D0C3FA34-2790-4896-96F7-E0E7C3E17687",
        "date" : 1.497918812348642E9,
        "huer" : 0,
        "latitude" : 32.87915823052905,
        "longitude" : -117.2140478064466,
        "type_" : "Video",
        "uid" : "BjN8usKaW7NhqD2DxPPqAkjhnSx2"
      }
    }

例如,我希望只能得到孩子“149791381894099”。 我目前正在使用的 swift 代码是(对不起,格式很奇怪)

self.rootRef.child(uidOtherProfile).child("time").queryEqual(toValue: 149791381894099).observeSingleEvent(of: .value, with: { (snapshot) in})

但是我用过

self.rootRef.child(uidOtherProfile).child("time").queryEqual(toValue: "149791381894099").observeSingleEvent(of: .value, with: { (snapshot) in

【问题讨论】:

  • 您的 JSON 不包含 time 属性。它确实包含 date 属性。那是您要过滤的内容吗?因为如果是这样:.child("time").queryOrdered(byChild:"date").queryEqual(toValue: 149791381894099)(请注意最后一个值周围没有引号)。
  • 感谢您的回复。但是不,我想按时间子项的值或日期值进行排序。不确定您是否注意到,但我与节点的“名称”具有相同的值作为日期。我想避免 .queryOrdered(byChild:"date") 因为我已经设置了数据条目,以便 firebase 会自动正确排序,我不想浪费计算。我只想使用“queryStarting、queryEnding、queryEqual”查询给定的日期值。
  • 使用self.rootRef.child(uidOtherProfile).child("time").child("149791381894099") 可以更轻松地完成您现在拥有的一切。但是有什么问题呢?
  • 我不仅关心 queryEqual,还关心 queryStarting 和 queryEnding。他们都遵循相同的机制(或者我相信)。我的总体目标是将 queryLimited(toFirst) 与 .queryStarting 结合使用,但目前我只询问一个部分。示例为 self.rootRef.child(uidOtherProfile).child("time").queryStarting(atValue: 149791381894099).queryLimited(toFirst:1).observeSingleEvent(of: .value, with: { (snapshot) in} )

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

Firebase 的默认排序顺序是按优先级排序。查询密钥需要调用queryOrderedByKey():

ref.queryOrderedByKey().queryEqual(toValue: "149791381894099").observe(.childAdded, with: { (snapshot) in
    print(snapshot)
})

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多