在Firebase响应中获取Firebase子节点的名字,而不让他们的孩子得到? [英] Get Firebase child nodes' names without getting their children too in Firebase response?

查看:121
本文介绍了在Firebase响应中获取Firebase子节点的名字,而不让他们的孩子得到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firebase上有以下层次结构,一些数据被隐藏起来以保密:


我试图获取视频ID列表(红色下划线)

>

我只能得到所有节点,然后检测它们的名字并将它们存储在一个数组中!因为来自firebase的 dataSnapshot 对我来说是非常大的,所以我想避免检索所有节点的内容,然后遍历它们来获取ID,我只需要检索只有ID,即没有它们的嵌套元素。



这里是我的代码:

 'value',
function(dataSnapshot){

// dataSnapshot现在包含所有视频id,行和链接
//这会导致很多性能问题

//然后我需要遍历所有元素来提取id!
var videoIdIndex = 0;
var videoIds = new Array();

dataSnapshot.forEach(
function(childSnapshot){
videoIds [videoIdIndex ++] = childSnapshot.name );
}
);

}
);

如何检索唯一ID以避免大量数据传输并避免循环检索数据获取ID?有没有办法直接检索这些ID?

解决方案

更新:现在有一个浅的命令在REST API中将只提取一个路径的键。这还没有添加到SDK中。



在Firebase中,如果不检索下面的数据,就无法获取节点名称列表。还没有呢。性能问题可以通过规范化来解决。

实质上,您的目标是将数据拆分为易损件。在一个路径中存储视频密钥的列表,可能包含标题等多个元字段,并将批量内容存储在其他位置。例如:

  / video_meta / id / link,title,... 
/ video_lines / id / .. 。

要了解有关反规范化的更多信息,请查看以下文章:https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html


I have the following hierarchy on firebase, some data are hidden for confidentiality:

I'm trying to get a list of videos IDs (underlines in red)

I only can get all nodes, then detect their names and store them in an array!
But this causes low performance; because the dataSnapshot from firebase is very big in my case, so I want to avoid retrieving all the nodes' content then loop over them to get IDs, I need to just retrieve the IDs only, i.e. without their nested elements.

Here's my code:

new Firebase("https://PRIVATE_NAME.firebaseio.com/videos/").once(
    'value', 
    function(dataSnapshot){ 

        // dataSnapshot now contains all the videos ids, lines & links
        // this causes many performance issues

        // Then I need to loop over all elements to extract ids !
        var videoIdIndex = 0;
        var videoIds = new Array();

        dataSnapshot.forEach(
            function(childSnapshot) {
                videoIds[videoIdIndex++] = childSnapshot.name();
            }
        );

    }
);

How may I retrieve only IDs to avoid lot of data transfer and to avoid looping over retrived data to get IDs ? is there a way to just retrive these IDs directly ?

解决方案

UPDATE: There is now a shallow command in the REST API that will fetch just the keys for a path. This has not been added to the SDKs yet.

In Firebase, you can't obtain a list of node names without retrieving the data underneath. Not yet anyways. The performance problems can be addressed with normalization.

Essentially, your goal is to split data into consumable chunks. Store your list of video keys, possible with a couple meta fields like title, etc, in one path, and store the bulk content somewhere else. For example:

/video_meta/id/link, title, ...
/video_lines/id/...

To learn more about denormalizing, check out this article: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

这篇关于在Firebase响应中获取Firebase子节点的名字,而不让他们的孩子得到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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