Konstantin Konopko
Konstantin Konopko

Reputation: 5422

Optimal way to get last posts of user's friends in Firebase

In my DB each user has friends. And I need to get 50 last posts of the user's friends. See below structure of posts of each user in Firebase DB.

So what is optimal way to do that? If a user has for example 150 friends, then my Android app should have as many listeners as friends. Is it ok to have 150 or more DB listeners in the app? Also what is the way to get all friends' posts in single query? Thanks.

root
- posts
  - {userUid}
    - {postUid}
      - text
      - dateCreated

Upvotes: 1

Views: 815

Answers (3)

Alex Mamo
Alex Mamo

Reputation: 138969

You can have as many listeners as you want with the condition to remove them according to your life-cycle of your activity. But in your case, you don't need 150 listeners, you only need just one. Assuming that the node under the user is is named friends, you can attach a single listener on friends node and then iterate over the DataSnapshot object using getChildren() method. Assuming that your database looks simmilar with this:

Firebase-root
  |
  --- users
       |
       --- uid1
            |
            --- friends
                  |
                  --- friendId1: true
                  |
                  --- friendId2: true

To get all the friends that corespond to a particular user, please use the following code:

String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference friendsRef = rootRef.child("users").child(uid).child(friends);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String friendId = ds.getKey();
            Log.d("TAG", friendId);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
friendsRef.addListenerForSingleValueEvent(eventListener);

To get all post that of all users, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference postsRef = rootRef.child("posts");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String uid = ds.getKey();

            DatabaseReference uidRef = postsRef.child(uid);
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
                        String text = dSnapshot.child("text").getValue(String.class);
                        long dateCreated = dSnapshot.child("dateCreated").getValue(Long.class);
                        Log.d("TAG", text + " / " + dateCreated)
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {}
            };
            uidRef.addListenerForSingleValueEvent(eventListener);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(valueEventListener);

Upvotes: 1

Frede
Frede

Reputation: 701

You can have 150+ listeners but the less the better. Maybe you can just watch all posts instead of listening to every friend separtely.

To get all posts of a friend once try:

ref('posts').child(friendUid).once('value').then(function(snapshot) {
   console.log(snapshot.val());
});

To get the last 50 posts of a friend once try:

ref('posts').child(friendUid).orderByChild("dateCreated").limitToLast(50).once('value').then(function(snapshot) {
   console.log(snapshot.val());
});

(depending on your dateCreated property you might need to use limitToFirst instead of limitToLast. Check the Firebase Query docu for more information.)

Upvotes: 0

Edward DiGirolamo
Edward DiGirolamo

Reputation: 762

What I would do is have a friendsListener, once initialized I would loop through my friends and register their reference in my listener vs 150 listeners.

Upvotes: 0

Related Questions