Reputation: 1
I am building an app where you can send and get friend request. The database is structured as:
Now I want to build an "alert function" when someone sends you a friend request with an addValueEventListener so that I can see if the current user have a "recieved" in the database. Something like:
mFriendRequestDatabase =
FirebaseDatabase.getInstance().getReference().child("Friend_req");
mCurrent_user = FirebaseAuth.getInstance().getCurrentUser();
String current_user_id = mCurrent_user.getUid();
mFriendRequestDatabase.child(current_user_id).addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
The problem is that i can't reach to see if current user have a "sent" or a "recieved" since i don't know the ID of the person that sent the request. dataSnapshot.child(???).child("sent" or "recieved")
How to I solve this problem?
Upvotes: 0
Views: 202
Reputation: 598728
You could get some of this with a query, but it'll be tricky to keep the performance under control since you'll need to define an index on every user. For some of the problems with this see my answers here, and here.
A better way is to separate the requests into two separate lists: the ones that were sent, and the ones that were received. E.g.
friend_requests
uidOfChristoffer
received
uidOfPuf: true
sent:
uidOfSomeoneElse: true
uidOfPuf
sent
uidOfChristoffer: true
uidOfSomeoneElse
received
uidOfChristoffer: true
Now you can specifically get the ones that anyone has received by reading /friend_requests/theirUid/received
. This is a more specific approach of a many-to-many relationship, so I also recommend reading my answer on that here.
Upvotes: 1