Firebase - What is the difference between ref and child?

16,569

Solution 1

There is no difference, in any case you have a DatabaseReference instance.

A Firebase reference represents a particular location in your Database and can be used for reading or writing data to that Database location.

The method:

public DatabaseReference getReference (String path)

Gets a DatabaseReference for the provided path.

The method:

public DatabaseReference child (String pathString)

Get a reference to location relative to this one.

Solution 2

There is no difference between the two ways you wrote. The only thing is that the first one is a shorthand, where "/" kinda means "child". By the way, the title question you put is not what your question is actually about. "Ref" and "child" are completely different things, because "ref" just makes a reference indicating where the data should go and "child" is specifying the more exact location of that traveling data. I would recommend to change that.

Share:
16,569
gegobyte
Author by

gegobyte

Talk is cheap, show me the code. Email:- [email protected]

Updated on June 07, 2022

Comments

  • gegobyte
    gegobyte about 2 years

    In Firebase, ref and child are used a lot.

    For example - firebase.database().ref('users/<user-id>') will work exactly same as firebase.database().ref('users').child('<user-id>'), so what exactly is the difference between them and when should either of them be used?

  • Jacob Ford
    Jacob Ford over 6 years
    When you say in the last line "relative to this one", what is "this one"?
  • Raphael St
    Raphael St over 6 years
    The one you're pointing at : the child of firebase.database().ref('users')
  • toraritte
    toraritte almost 6 years
    Would you elaborate on the difference with some examples? Upvoted, because I also felt that they differ, but I still can't put on a finger on it.
  • Suren
    Suren about 5 years
    For example, when you say firebase.database().ref() this just tells the code to visit the database in general without telling any specific location, but when you add a child, such as firebase.database().ref(users/<user-id>) you tell the code to visit the exact location.In general, "ref" and "child" come together, but the meanings are different.
  • appu
    appu over 4 years
    @Suren as you say Ref and child are completely different things, So ref() is a method to visit any location while child() is to visit a specific location so they both visit/traverse to a location, just that child() is more specific, thus IMHO they are not completely different things.
  • appu
    appu over 4 years
    @Suren Also as you say, "firebase.database().ref() this just tells the code to visit the database in general", actually firebase.database().ref() represents the root location of your database. see 2nd Para on firebase ref docs
  • Suren
    Suren over 4 years
    @appu that's correct, it visits the root location, I just wanted to explain it in different words by saying "database in general", because I think different wordings provide more choices for people to understand if they don't feel comfortable with official doc wordings, of course as long as they convey the correct information as well.
  • Suren
    Suren over 4 years
    @appu what refers to ref() and child(), they are completely different in a meaning that ref() is the one who actually creates the connection to database, after which you can use child() to specify the exact location. Without ref(), child() is basically useless