Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve multiple keys in Firebase? [duplicate]

Considering this data structure:

{
    "users":{
        "user-1":{
            "groups":{
                "group-key-1":true,
                "group-key-3":true
            }
        }
    },
    "groups":{
        "group-key-1":{
            "name":"My group 1"
        },
        "group-key-2":{
            "name":"My group 2"
        },
        "group-key-3":{
            "name":"My group 3"
        },
        "group-key-4":{
            "name":"My group 4"
        },
    }
}

I could get the groups of a particular user with:

firebase.database().ref('users/user-1/groups').on(...)

Which would give me this object:

{
    "group-key-1":true,
    "group-key-3":true
}

So now I want to retrieve the info for those groups.

My initial instinct would be to loop those keys and do this:

var data = snapshot.val()
var keys = Object.keys(data)

for (var i = 0; i < keys.length; i++) {
    firebase.database().ref('groups/' + keys[i]).on(...)
}

Is this the appropriate way to call multiple keys on the same endpoint in Firebase?

Does Firebase provide a better way to solve this problem?

like image 342
Pier Avatar asked Jul 04 '16 21:07

Pier


People also ask

How do I find my unique key in firebase?

You can create a unique key in Firebase database using push() and then add child nodes under that key. Now next time when you come to that activity, first check if parent node exists or not. If the node exists, save parent node key and use it to save new child nodes.

Which method is used to fetch any key from Firebase?

We can use HashMap.

What is DataSnapshot in firebase?

firebase. database. DataSnapshot. A DataSnapshot contains data from a Database location. Any time you read data from the Database, you receive the data as a DataSnapshot .


2 Answers

Is this the appropriate way to call multiple keys on the same endpoint in Firebase?

Yes, generally this is a good solution to retrieve every group this way.

Does Firebase provide a better way to solve this problem?

I don't think Firebase provides any other function/query that could help in this case.

Anyway, this could be improved saving the ref in a variable and looping on the keys of the object directly. Also, if you just need to retrieve those data once, you should use once() instead of on()

var groupRef =  firebase.database().ref('groups/')
var data = snapshot.val()

for (var groupID in data) {
    groupRef.child(data[groupID]).once(...)
}

Another way could be using Firebase's functions for the data snapshots, like forEach

snapshot.forEach(function(childSnapshot) {
      groupRef.child(childSnapshot.key).once(...)
})
like image 90
Devid Farinelli Avatar answered Oct 19 '22 14:10

Devid Farinelli


Yes, you are going in the right way. As written in this question firebase will pipeline your requests and you won't have to be concerned about performance and roundtrip time. As soon as your iteration starts you will be receiving firebase data. So keep in mind to handle it properly in your ui.

Another option, that will depends on how big your /groups data will grow, is to keep a snapshot (could be a $firebaseObject if you are using angularfire) of the entire /groups branch that will refresh whenever data changes. Then you just have to track this snapshot. But again, if you are planning to play with a big amount of groups your current solution is a better choice.

Also, I recommend you to take care of setting an on event for each group you retrieve. Keep in mind that the callback will be triggered whenever the data changes (depends on the setted event). Depending on your use case you should consider using .once("value" since it will trigger the data only once, making it more reliable, more performatic and will avoid handling callbacks when you are not expecting them.

like image 26
adolfosrs Avatar answered Oct 19 '22 13:10

adolfosrs