Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Firebase child nodes' names without getting their children too in Firebase response?

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

enter image description here

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 ?

like image 918
Ashraf Bashir Avatar asked Jun 20 '13 10:06

Ashraf Bashir


2 Answers

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

like image 114
Kato Avatar answered Nov 02 '22 06:11

Kato


It is a bit old, and you probably already know, but in case someone else comes along. You can do this using REST api call, you only need to set the parameter shallow=true

here is the documentation

like image 39
webduvet Avatar answered Nov 02 '22 07:11

webduvet