Skip to content Skip to sidebar Skip to footer

How To Retrieve Firebase Database For Specific User Only?

I am using Firebase Database for my android application. Login feature is available. There is a bookmark/favourite option in the app. After pressing the bookmark button the data ar

Solution 1:

You need to access the user uid, so only that user's data will be shown:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userId=user.getUid();

//String.valueOf(int)
mUserDatabase = FirebaseDatabase.getInstance().getReference("Bookmarks").child(String.valueOf(10)).child(userId);

getUid() will retrieve the id of the current logged in user.

Solution 2:

This is not a good way of storing the favorites for each user. Instead, change your database structure to:

BookMarks   
    |UserUID
        |10
        |3
    |UserUID2
        |6
        |10

This structure will help you to view all the favorites of one user at a particular time. Your current structure will limit you to viewing one favorited item for one user or all users who have favorited that item

Post a Comment for "How To Retrieve Firebase Database For Specific User Only?"