Skip to content Skip to sidebar Skip to footer

Firebase Show If Child Exists

I am using FirebaseIndexRecyclerAdapter and I'am trying to show post for the following users so I have used these method blew but It show posts if the post id is available at the f

Solution 1:

To check if a fiven reference have childrens you should use dataSnapshot.exists() or dataSnapshot.getChildrenCount() > 0 with the given DataSnapshot on your callback.

If you want to retrieve the post which id is equal to the followers you should modify your database adding an extra level to pur Posts table where the post will be already together based on a user id:

"Posts":{"USER_ID_1":{"post_id_1":{"Describe":"gg","Time":14881230655,"UID":"pypD1SYZkbcYesk09WuMUY1AkTf1","Username":"Jone"},"post_id_2":{"Describe":"gg","Time":14881230655,"UID":"pypD1SYZkbcYesk09WuMUY1AkTf1","Username":"Jone"}},"USER_ID_2":{"post_id_3":{"Describe":"gg","Time":14881230655,"UID":"pypD1SYZkbcYesk09WuMUY1AkTf1","Username":"Jone"},"post_id_4":{"Describe":"gg","Time":14881230655,"UID":"pypD1SYZkbcYesk09WuMUY1AkTf1","Username":"Jone"}}

To iterate looking to get all the posts inside each of your user's(just if you are following the new data model that I post you upside), you can retrieve them doing this:

        List<Getting_Posts> totalPosts = new ArrayList<>();
        //Check if the dataSnapshot have childrensif (dataSnapshot.hasChildren()){
           //This for iterate for each USER_ID from your databasefor (DataSnapshot users : dataSnapshot.getChildren()) {
              //This second iterate for each post for each userfor (DataSnapshot posts : users.getChildren()) {
                 totalPosts.add(dataSnapshot.getValue(Getting_Posts.class));
              }
           }
        }

        return totalPosts;

You can read more about this in the next post or in the oficial documentation: https://www.airpair.com/firebase/posts/structuring-your-firebase-data

https://firebase.google.com/docs/database/web/structure-data?hl=en-419

Post a Comment for "Firebase Show If Child Exists"