【问题标题】:Firebase access keys in queryOrderByqueryOrderBy 中的 Firebase 访问密钥
【发布时间】:2016-06-04 15:33:58
【问题描述】:

我有 2 个与此查询相关的问题:

  1. 我在 Firebase 中运行查询,查询返回我正在寻找的结果,但是当我尝试访问查询中的数据时,结果为 nil

  2. 如何访问查询中的标签键?它是 1 深

提前谢谢你

查询

ref.child("users")
   .queryOrderedByChild("receivePostRequest/status")
   .queryEqualToValue(true)
   .observeEventType(.Value, withBlock: {snapshot in

JSON 结果

Optional({
    lgmSZ1HnMnSzE71kCLfdxK8AN2G2 =     {
        age = 18;
        email = "lon1@gmail.com";
        firstname = Jamie;
        lastname = lon;
        latitude = "37.3325232";
        longitude = "-122.0286527";
        profilePic = "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg";
        receivePostRequest =         {
            status = 1;
            tag = tagSample;

        };
        userId = lgmSZ1HnMnSzE71kCLfdxK8AN2G2;
    };
})

这是完整的查询

ref.child("users")
   .queryOrderedByChild("receivePostRequest/status")
   .queryEqualToValue(true)
   .observeEventType(.Value, withBlock: {snapshot in
             print(snapshot.value["firstname"])
        })

JSON

{
  "posts" : {
    "-KJGom2RvmWkfbvsFXij" : {
      "postAddress" : "Post Address",
      "postCompletionAddress" : "post completion address",
      "postCreationTime" : "postCreation Time",
      "postDateTime" : "Date and Time",
      "postDescription" : "Post Detail",
      "postPay" : "post Pay",
      "postTitle" : "Post Title",
      "status" : "pending",
      "userAiD" : "W6pK2HHA1TZC9wicnCaODQhHvoi1",
      "userBiD" : "lgmSZ1HnMnSzE71kCLfdxK8AN2G2"
    },
  },
  "users" : {
    "W6pK2HHA1TZC9wicnCaODQhHvoi1" : {
      "age" : 18,
      "email" : "ama@gmail.com",
      "firstname" : "jam",
      "lastname" : "lime",
      "latitude" : 37.332172,
      "longitude" : -122.035089,
      "profilePic" : "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg",
      "receivePostRequest" : {
        "lat" : 28.10277584477151,
        "latitude" : 37.33067237,
        "long" : -81.4587166999294,
        "longitud" : -122.03014382,
        "status" : false,
        "tags" : {
          "tag1" : 444
        }
      },
      "userId" : "W6pK2HHA1TZC9wicnCaODQhHvoi1"
    },
    "lgmSZ1HnMnSzE71kCLfdxK8AN2G2" : {
      "age" : 18,
      "email" : "weirhe@gmail.com",
      "firstname" : "james",
      "lastname" : "leen",
      "latitude" : 37.3325232,
      "longitude" : -122.0286527,
      "profilePic" : "users/profilePhoto/W6pK2HHA1TZC9wicnCaODQhHvoi1.jpg",
      "receivePostRequest" : {
        "status" : true,
        "tag" : "tag 1"
      },
      "userId" : "lgmSZ1HnMnSzE71kCLfdxK8AN2G2"
    }
  }
}

【问题讨论】:

  • 您可以发布您的 Firebase 结构的 sn-p 吗?请作为文本,没有图像(Firebase Dashboard->三个点(?)->导出
  • snipped 已添加到编辑中,谢谢。

标签: swift firebase


【解决方案1】:

小心 1 和 true,因为它们会返回不同的结果

如果您在 Firebase 中存储了一个整数值 1

node_0
  test: 1
node_1
  test: true
node_2
  test: 1

如果查询是

queryOrderedByChild("test").queryEqualToValue(true)

只有 node_1 会被返回,因为它是真的

同样

queryOrderedByChild("test").queryEqualToValue(1)

只有 node_0 和 node_2 将被返回,因为它们是 1

然后回答这个问题:

print(snapshot.value) //prints all of the data in the node

当您返回 .Value 时,快照将有孩子,因此访问每个孩子的键的一种方法是:

for child in snapshot.children {
  let key = child.key as String
  print(key)
}

这将打印每个父节点密钥,在这种情况下为 lgmSZ1HnMnSzE71kCLfdxK8AN2G2

编辑

回应评论/问题

但我也在尝试检索"tag" : "tag 1

最初没有明显的方法可以从 child = snapshot.children 循环中获得,所以这里是你如何做的

for child in snapshot.children {

     let receivePostRef = child.childSnapshotForPath("receivePostRequest")
     let aTag = receivePostRef.value["tag"]
     print("setting = \(aTag)") //print's Tag1
}

【讨论】:

  • 你的建议确实适用于第一部分,但我也在尝试检索`“tag”:“tag 1` 我试过这个for child in snapshot.children { print(child.value["receivePostRequest/status"]) let key = child.key as String print(key) }
  • @peter 这是一个非常棒的问题,我花了一段时间才想通。我已经用附加答案更新了我的答案。我建议检查您的数据结构,因为您的 receivePostRequest 节点不一致。格式因用户而异,这会让您感到悲伤;一个有一个“标签”节点,一个没有。要么让所有人都有一个标签:有孩子的节点,要么让他们都只有一个标签孩子。
  • 您的回答很好,已被接受。我也将更新数据库
【解决方案2】:

试试..

print(snapshot.childSnapshotForPath("firstname").value)

【讨论】:

  • 我得到一个 Optional()
  • 好吧,只是为了更好地了解情况,如果您使用 print(snapshot.value) 您会在问题中得到 JSON 结果,对吗?
  • 请详细说明这段代码是如何回答这个问题的。
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 2017-03-23
  • 2021-12-25
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-09-12
相关资源
最近更新 更多