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

1

What is the difference between ref() and child() in firebase? When should I use one or the other? Are there any performance differences?

    
asked by anonymous 03.03.2018 / 02:06

1 answer

1

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

Let's see how these methods work:

  • ref() - returns a reference to the database node passed as parameter. If you do not have any parameters specified, it returns the reference to the root node of the database.
  • child() - also returns a reference to a database node. But the difference is that it serves to access the sub-nodes within a Reference.
  • Given that child() is of the Reference class and also returns a Reference object, you can use childs chained as follows:

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

    But the same does not happen with ref() . Can not make ref.ref("users").ref("uid1").ref("nome") .

    You can also access the sub-nodes using ref() :

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

    And using 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.

        
    03.03.2018 / 10:13