0

Suppose that I have a data store like this.

Firebase database

but I want only username and profileImage. What method should I use for this case?

The first one is query data in two times. Something like this...

usersRef.child(uid).child("username").addValueListener...    
String username = dataSnapshot.getValue(String.class);    
usersRef.child(uid).child("profileImage").addValueListener...
String profileImage = dataSnapshot.getValue(String.class);

or

The second one is get all children only one time and get only data that I want. Something like this...

 - userRef.addValueListener...
 User user = new User();
datasnapshot.getValue(user.class)
String username = user.username;  
String profileImage = user.profileImage;
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
JustKhit
  • 407
  • 3
  • 9

1 Answers1

0

The overhead on separate calls (in this scenario) is fairly small. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786.

The overhead for retrieving extra data, depends on the size of that extra data. In this scenario seems rather large, especially since you have nested collections in there. This is one of the many reasons Firebase recommends flattening our data, moving all your nested collections to their own top-level nodes (userFollowers, userFollowings, userNoficationTokens).

For optimal performance you should have a list that contains the data as you want to display in the app. So if you have a screen that shows a list of user names + images, you should have a list with just that data in the database. You'll find that you're duplicating data in that case, but you gain almost unlimited scalability because of it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807