Rishabh Chandel
Rishabh Chandel

Reputation: 745

Firebase Android: Retrieve post from current user's friends only

I'm using FirebaseRecyclerAdapter to show posts. But I don't want to show all the posts. I just want to show posts which are posted by current user's friends. Current User's friends in Realtime Database is stored in this way:

enter image description here

And Every post is stored with the uid of User who posted it, in following way:

enter image description here

So, what will be the efficient way to retrieve posts from only current user's friends.

Upvotes: 1

Views: 2107

Answers (2)

Rishabh Chandel
Rishabh Chandel

Reputation: 745

Solution for this issue is use FirebaseIndexRecyclerAdapter instead of FirebaseRecyclerAdapter. Link: https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-with-indexed-data

Step by Step:

First-> maintain post of every user in a node let say "My_Challenges": enter image description here

So whenever a user post anything, copy that post id under user uid inside this node too.

Second-> Maintain another node for posts which you can see(in my case i want to see only my friends posts) let say "timelineIndex": enter image description here

Let say User 1(1014382358695053) and User 2(10207254337991322) are friend. So copy all post id from My_Challenges of User 1 to timelineIndex of User 2 and vice versa also. (NOTE : You have to do this every time a new post id comes under your uid copy it to your friend uid under timelineIndex)

Finally-> Use FirebaseIndexRecyclerAdapter : As in above given link, use

keyRef = databaseReference.child("timelineIndex").child(mUserId);

dataRef = databaseReference.child("Blogs");

That's it. I hope it helps.

Upvotes: 2

JordanH
JordanH

Reputation: 190

To prevent people from seeing the posts you should use some method of authentication. This way the only people that can see the posts are the people who are logged in. Firebase one to one messaging is not directly supported as stated in this previous question:

How to send one to one message using Firebase Messaging

Upvotes: 0

Related Questions