Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getReference() vs. getChild()

I was wondering what the difference between database.getReference("foo/bar/123") and database.getReference("foo").child("bar").child("123") is?

I'm assuming that the later one will load the complete "foo" object whereas database.getReference("foo/bar/123") just loads the "123" object?

Is my assumption correct or what is the correct / most efficient way to only load data of "123"?

like image 889
sockeqwe Avatar asked Mar 02 '17 09:03

sockeqwe


People also ask

How do you get a reference to a child in Firebase?

You can reference the root or child location in your Database by calling firebase. database(). ref() or firebase. database().

What does getKey () do Firebase?

Calling the getKey() method on this reference will return the auto-generated key which may then be used to store a corresponding value. Using Firebase to generate unique keys in this way is also of particular use when an app has multiple users creating child nodes at the same path in the tree.

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.


2 Answers

The two are equivalent. You can inspect this manually this by printing the toString() format for both References.

References are cheap - there's nothing inefficient about either solution. Neither one has yet loaded any data. A Reference is just a pointer to a location in the database.

like image 150
Doug Stevenson Avatar answered Sep 20 '22 01:09

Doug Stevenson


It should not make a difference, a reference is not actually accessed when instantiated. This is the most relevant document I can find,

https://firebase.google.com/docs/reference/node/firebase.database.Reference

The docs don't say it explicitly, but requests are only performed when using the .set() or .on() methods

like image 36
AtaerCaner Avatar answered Sep 23 '22 01:09

AtaerCaner