sooon
sooon

Reputation: 4878

firebase - get child database reference with key

I am using Firebase for my web app and fairly new with it. As I read the documentation, it mentioned that in order to minimize the size of download (for cost saving reason), we should flatten the data structure so that we will not download unnecessary data. But throughout the doc, they will always reference the whole database first:

dbRef = firebase.database().ref();

then only sample the data with key:

childRef = dbRef.child(child_key)

I am not sure I got this right but at least this is how I understand it.

My question is, won't that dbRef already taken the whole database down? Or the download only happen during the childRef in the scenario above?

Any information will be helpful as I google and found some nightmare cases with unbelievable price because of this database issue that is not handle correctly.

  1. Is there other issue that I need to worry about now since I am at the beginning stage of development?

Upvotes: 1

Views: 1500

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317372

A Reference that you get from ref() and child() is just a pointer into a location in the database. It's exceptionally cheap, and creating one doesn't perform any data access.

If you want to actually fetch the data from a reference, you have to call on() or once() on it. Until then, all you have is a tiny object that contains a location. Same thing with Query objects. They don't perform any queries until you call one of those same methods.

Upvotes: 1

Producdevity
Producdevity

Reputation: 882

There is a difference between a database ref and the actual database data request ( on('value') and once('value'))

The database reference is representing a particular location (or child/node/ref) in your database. The moment you call one of these methods on the Reference object (child() also returns a Reference object) you are actually fetching data, which is the expensive part. Besides that, it's always a good thing to have just one variable thats holding a reference.

Upvotes: 1

Related Questions