【问题标题】:Firebase query between two latitudes两个纬度之间的 Firebase 查询
【发布时间】:2016-06-27 01:33:12
【问题描述】:

当帖子的纬度在用户当前纬度的 +/- 0.1 范围内时,我无法从我的数据库接收数据。

我的 Firebase 树如下:

{
"person" : {
  "ss" : {
    "email" : "contact@surrogatesystems.com",
    "username" : "Surrogate Systems"
  },
  "CZ6Ujxsm0SXybtcLBthXOpG3YgU2" : {
    "email" : "ljahnke9@gmail.com",
    "username" : "Logan Jahnke"
  }
},
"prayers" : {
  "p0" : {
    "contents" : "For understanding of Firebase as we build the Surge: Prayer Sharing application.",
    "likes" : 100,
    "personid" : "ss",
    "posted" : "2016-06-21 20:15:00",
    "latitude" : 33.9441737581188,
    "longitude" : -83.3843067012304
  },
  "p1" : {
    "contents" : "This is a test prayer.",
    "likes" : 0,
    "personid" : "CZ6Ujxsm0SXybtcLBthXOpG3YgU2",
    "posted" : "2016-06-21 20:00:00",
    "latitude" : 33.9441737581188,
    "longitude" : -83.3843067012304
  },
  "p2" : {
    "contents" : "My prayer request is that every child in Los Andes, Panama know the love of Jesus Christ. I also pray for their families and their friends. I pray that their education runs smoothly and that they learn the English they desire to know. I pray that we made an impact on them.",
    "likes" : 0,
    "personid" : "CZ6Ujxsm0SXybtcLBthXOpG3YgU2",
    "posted" : "2016-06-21 20:00:00",
    "latitude" : 35.3341737581184,
    "longitude" : -81.3843067012304
}

} }

纬度和经度的类型为Double。我目前的查询是:

let lat = // user's latitude by using CLLocation -> ~33.9
let query = ref.child("prayers").queryStartingAtValue(lat - 0.1, childKey: "latitude").queryEndingAtValue(lat + 0.1, childKey: "latitude").queryLimitedToFirst(50)
// This should theoretically return a list of size 2

这会在调用快照时返回 NSNull,即使我的系统中有符合条件的整体。

我的电话出了什么问题,我该如何解决?

【问题讨论】:

  • 您能否从您的数据库中显示一些您希望与此查询匹配的实际 JSON 数据?您可以通过单击导出按钮轻松地从 Firebase 数据库控制台获取它。 Y 将 JSON 作为文本使其可搜索,使我们能够轻松地使用它来测试您的实际数据并将其用于我们的答案中,通常只是一件好事。

标签: swift firebase firebase-realtime-database


【解决方案1】:

我看到了同样的行为。通过插入对 queryOrderedByChild 的调用,我得到了预期的结果:

    let query = ref.child("prayers")
                .queryOrderedByChild("latitude")
                .queryStartingAtValue(lat - 0.1)
                .queryEndingAtValue(lat + 0.1)
                .queryLimitedToFirst(50)
                .observeEventType(.ChildAdded, withBlock: { (snapshot) in
                    print(snapshot)
                })

【讨论】:

  • observeEventType 对您来说是什么样的?我的是:query.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in,我的snapshotnull
  • 将其添加到答案中
  • 这似乎奏效了,然而,snapshot 只是一个分解成 6 个键(祈祷键)的祈祷。我需要snapshot[Prayer : Key] 而不是[Key : Value]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
相关资源
最近更新 更多