Ad

Which Is More Better Firebase Using Query Or Using Normal Reference

I noticed there are 2 types to retrieve a user data. Which one is better?

firebaseDatabase = FirebaseDatabase.getInstance();
usersDbRef = firebaseDatabase.getReference().child("Users");

//Using Query
Query userQuery = usersDbRef.orderByChild("uid").equalTo(userUid);


//Using Normal
DatabaseReference dbRefUser = usersDbRef.child(userUid);

//And then get user name and etc...
Ad

Answer

When you are using the following line of code:

usersDbRef = firebaseDatabase.getReference().child("Users");

You are getting all users that exist within Users node without any limitations.

If you are using a query, you can add limitations and ordering to your items. For example, let's say you have a node that contains 1000 items. For sure you won't use a simple reference because getting 1000 items at once, will be a waste of bandwidth and resources, since no users are interested in 1000 items. What you can do instead is to add a call to limit(50) and display only 50 items. So a query is needed to filter results and according to the use-case of your app, you can use one or the other in your project.

When using:

DatabaseReference dbRefUser = usersDbRef.child(userUid);

You tell Firebase that you are interested only in a single user object. So in this case, these is no need for a query, a simple reference will do the job.

Ad
source: stackoverflow.com
Ad