1

My objective is to get post from user1, user3, user4 which user2 is following.

 //Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child
            ("Users").child("following").child(uid);

    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    //Get map of users in datasnapshot

                    collectDispayNames((Map<String, Object>) dataSnapshot.getValue());
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    //handle databaseError
                }
            });




private void collectDispayNames(Map<String, Object> users) {

    displayNames = new ArrayList<>();
    //iterate through each following, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()) {

        //Get user map
        Map singleUser = (Map) entry.getValue();

        //Get following_id field and append to list
        displayNames.add((String) singleUser.get("who_i_follow"));

        //Toast.makeText(this, displayNames.toString(), Toast.LENGTH_LONG).show();

        int size = displayNames.size();
        if (size > 0){

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
        }else {

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents");
        }
        if (size >1){

            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));
        }else {
            mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                    .child("Contents");
        }

    }
}

Now let assume i retrieve all users which user2 is following into array called showOwnerId

I can get array of who user2 following, but since it has many people following, how is it possible to insert all the id of users it is following into database query like this

        mDatabase = FirebaseDatabase.getInstance().getReference().child("Post").child("Blog")
                .child("Contents").orderByChild("user_id").equalTo(displayNames.get(1));

1 Answers1

2

There is no way to query for multiple values in one go. You will have to run a separate query for each user that is followed, and merge the results client-side.

Since Firebase can pipeline these queries over a single connection, the performance is not nearly as bad as some developers think.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Could you please re-write the code in iOS to android(java)? I don't seems to understand the code because i wanted to display the data using `FirebaseRecyclerView` –  Jul 28 '18 at 15:21
  • I didn't share any iOS or Android code. But if you're having trouble wiring this up in your code, update your question to show what you've tried for that. But note that FirebaseUI does not have built-in support for showing multiple queries. If you need to do that, you will have to write the code yourself. – Frank van Puffelen Jul 28 '18 at 16:57