44

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
gegobyte
  • 4,945
  • 10
  • 46
  • 76

3 Answers3

30

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
8

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.

Suren
  • 173
  • 3
  • 11
  • 2
    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. – toraritte Jul 11 '18 at 22:24
  • 1
    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/)** you tell the code to visit the exact location.In general, "ref" and "child" come together, but the meanings are different. – Suren May 31 '19 at 14:16
  • 1
    @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 Oct 19 '19 at 08:04
  • 2
    @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](https://firebase.google.com/docs/reference/js/firebase.database.Reference) – appu Oct 19 '19 at 08:24
  • 1
    @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 Mar 31 '20 at 00:52
  • 3
    @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 – Suren Mar 31 '20 at 00:53
-1

In simple terms, ref only points to the memory location of a said data while child has the location. In other words, you can say a reference to a location is achieved by a child. However it's similar to a storage path where you have Users/22222/image.jpg

kelsny
  • 23,009
  • 3
  • 19
  • 48