如何遍历并获取 firebase 中嵌套节点的所有键? [英] How do I loop through and get all the keys of the nested nodes in firebase?

查看:32
本文介绍了如何遍历并获取 firebase 中嵌套节点的所有键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 Firebase 中嵌套节点的键,但我不确定如何执行此操作.

I am trying to get the keys of the nested nodes in Firebase and I am not sure how to do this.

例如在这种情况下:

示例

我怎么知道 1 中存在 2、3、4?

How do I know that 2,3,4 exist within 1?

我正在考虑将一个值分别放在 firebase 中的列表中.但是有没有更聪明的方法来做到这一点?有没有更有效的方法来获取 Firebase 中所有嵌套节点的键?

I am thinking of putting a values in a list seperately in firebase. But is there a smarter way of doing this? Is there a more efficient way of getting the keys of all the nested nodes in Firebase?

推荐答案

In Android

允许访问此快照的所有直接子级.可用于本机 for 循环:

Gives access to all of the immediate children of this snapshot. Can be used in native for loops:

for (DataSnapshot child : parent.getChildren()) { 
   //Here you can access the child.getKey()
}

在 iOS 中

斯威夫特 3:

for (child in snapshot.children) { 
  //Here you can access child.key
}

斯威夫特 4:

snapshot.children.forEach({ (child) in
  <#code#>
})

在网络中

snapshot.forEach(function(childSnapshot) {
   //Here you can access  childSnapshot.key
});

你可以把它放在不同的列表或相同的路径中,重要的是记住在调用事件时你真正检索了多少数据.以及你将如何查询这些信息......这就是为什么在 NoSQL 中建议保持扁平节点

You can put it in a different list or in the same path, the important thing is to keep in mind how much data you are really retrieving when calling an event. And how are you going to query that information... that is why it is recommended in NoSQL to keep flat nodes

这篇关于如何遍历并获取 firebase 中嵌套节点的所有键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆