Reputation: 372
I'm developing a chat base app in Firebase Android. I got 2 ways to get the same result, but not able to detect which one is better in performance. Need help with following.
DB Structure:
Firebase -
- User
- user id
- name
- DOB
- userId
I need to fetch the user info for selected key. So there are 2 ways as follows:
Create Firebase database reference until userId and add an event listener to it:
database.child("Users").child(userCurrent.getUserId()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Create a Firebase database reference until Users node and then try to find the exact user by using equalTo
query on key:
database.child("Users").orderByKey().equalTo(userCurrent.getUserId()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Will the second option fetch every user from User
node and then query for equalTo
?
Upvotes: 0
Views: 198
Reputation: 2266
You don't need to query if you can create exact reference to your user object. Querying is totally unnecessary in this case and of course worse performance.
The second option will not fetch every user on the Users node, it will just perform a query and return the user you are looking for.
Also if you don't want to listen for every change on the node I suggest using the addListenerForSingleValueEvent()
, which will read the value one time only.
Upvotes: 2