How To Retrieve Multiple Keys In Firebase?
Solution 1:
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/')
vardata = snapshot.val()
for (var groupID indata) {
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(...)
})
Solution 2:
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.
Post a Comment for "How To Retrieve Multiple Keys In Firebase?"