Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - What is the difference between ref and child?

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?

like image 629
gegobyte Avatar asked Jul 29 '16 04:07

gegobyte


People also ask

What is ref in Firebase?

A Reference represents a specific location in your Database and can be used for reading or writing data to that Database location. You can reference the root or child location in your Database by calling firebase. database(). ref() or firebase.

How do I find the number of children in Firebase?

Show activity on this post. retrieve childs of Day 1 to an array and count the array that ll be the no of child. Show activity on this post. add another lister to days that will give you all matches you played on a day.

What is DataSnapshot in Firebase?

A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.

What is addListenerForSingleValueEvent?

addValueEventListener() keep listening to query or database reference it is attached to. But addListenerForSingleValueEvent() executes onDataChange method immediately and after executing that method once, it stops listening to the reference location it is attached to.


2 Answers

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.

like image 177
Gabriele Mariotti Avatar answered Sep 29 '22 10:09

Gabriele Mariotti


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.

like image 43
Suren Avatar answered Sep 29 '22 10:09

Suren