ref() or Child() when reading firebase web data

Asked

Viewed 2,021 times

1

What’s the difference between ref() and child() firebase? When should I use one or the other? Is there any difference in performance?

1 answer

1


The 2 are methods of different classes, but return the same thing: a Reference. The ref() is of class Database and the child() class Reference.

Let’s see how these methods work:

  1. ref() - returns a reference to the database node passed as parameter. If it has no specified parameter, it returns the reference to the root node of the database.
  2. child() - also returns a reference to a database node. But the difference is that it serves to access the sub-nodes within a Reference.

Bearing in mind that the child() is from the Reference class and also returns a Reference object, you can use chained Childs as follows:

ref.child("users").child("uid1").child("nome")

But the same does not happen with the ref(). Is not possible do ref.ref("users").ref("uid1").ref("nome").


It is also possible to access the sub-nodes using the ref():

ref = database.ref("users/uid1/nome");

And using the child():

ref = database.ref().child("users/uid1/nome");
//ou até
ref = database.ref("users").child("uid1/nome")

In short: In the end, the 2 methods return the same thing and have no difference in terms of performance. Use the method that makes you more comfortable.

  • There’s a difference in performance chlid('a/b') and child('a').child('b')?

  • There is nothing documented by Firebase. But if you have any difference will be minimal and negligible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.