2

I want to populate all posts of user and their friends. My Database implementation is attached

User Implementation User implementation

Posts Implementation Posts Implementation

Text Json:-

{
  "UserTable" : {
    "KhusbuDOTaATgDOTcom" : "-KIcHQ66Vig1aUjmWIfg",
    "aDOTkATgDOTcom" : "-KIYwF0kWZ97pkm0od-F",
    "amATaDOTa" : "-KIa86JUxWbDnqStdDxh",
    "amanATaDOTa" : "-KIa9eEbHu4YNGnjpbZv",
    "arnabDOTdaATgmailDOTcom" : "-KIYu5eUE04if17exaYe",
    "imakshayboraATgmailDOTcom" : "-KIT9PnIJbOGigKT1BHa",
    "newuserATgmailDOTcom" : "-KJ12Z9IZNz_kGNIcPs1",
    "rameshDOTtATgmailDOTcom" : "-KIa0kfOzm_hlUJ_pBbV",
    "sovanDOTgATgmailDOTcom" : "-KIT9WWG--HaRzCm6fg_",
    "test1ATuserDOTcom" : "-KJEgt7DD125dBgwSPSh",
    "testATtDOTt" : "-KIa4-dHS7-iVT9b2Jh4"
  },
  "message" : "Do you have data? You'll love Firebase.",
  "message1" : "akshay is the best",
  "posts" : {
    "-KIa9UA-FUMSDg4uwVTf" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9760896 77.7576878",
      "post" : "hi",
      "timestamp" : 1464150389786
    },
    "-KIa9XhunDYdVzj0g8Dx" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9762825 77.7577278",
      "post" : "hello",
      "timestamp" : 1464150404299
    },
    "-KIcHFDcUuUu8nUO8Wg2" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9757344 77.7575097",
      "post" : "hi Khusbu ",
      "timestamp" : 1464185980168
    },
    "-KIi3chQa1WW_K3JL_O5" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.976163 77.7577681",
      "post" : "hi bolo",
      "timestamp" : 1464283075118
    },
    "-KIi7ab1al65UBp4IXDB" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "hi",
      "timestamp" : 1464284113805
    },
    "-KIi7dxmgIwbJnl2KnBP" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "hibn",
      "timestamp" : 1464284127520
    },
    "-KIi83HBrUaElIwj5bT1" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "aka ",
      "timestamp" : 1464284236238
    },
    "-KIi8PeO_T00FA3bRYm_" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761519 77.7577668",
      "post" : "ji",
      "timestamp" : 1464284326992
    },
    "-KIiDwSd7H65RDilOZ00" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9760913 77.7577808",
      "post" : "hj",
      "timestamp" : 1464285776169
    },
    "-KImYPEKRS4DqwnOvi-W" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "mLocation" : "12.9761887 77.7576894",
      "post" : "hi",
      "timestamp" : 1464358249771
    },
    "-KImcjHn6vtkYmwRUiFF" : {
      "author" : "-KIa9eEbHu4YNGnjpbZv",
      "mLocation" : "12.9757941 77.7578351",
      "post" : "mat dikhana isko",
      "timestamp" : 1464359646738
    },
    "-KImhPxhaUcvs228_sQ6" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761605 77.7577076",
      "post" : "yum hai hum",
      "timestamp" : 1464360874200
    },
    "-KImmbSE0ytEDxdrvt4K" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9756235 77.7576516",
      "post" : "goodnight ",
      "timestamp" : 1464362237046
    },
    "-KImrq-fVNYTZZ_7aWkG" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761095 77.7577431",
      "post" : "test1",
      "timestamp" : 1464363606381
    },
    "-KImsBvI_v0ChkTfNr2H" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761095 77.7577431",
      "post" : "test2😃",
      "timestamp" : 1464363700223
    },
    "-KIrKTvw8oSWfE07R6WM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761034 77.7577107",
      "post" : "test3",
      "timestamp" : 1464438485113
    },
    "-KIrKsN-2HRpwbRz_1nq" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9761034 77.7577107",
      "post" : "test4",
      "timestamp" : 1464438589263
    },
    "-KIrLHKJeuqxb4Wp48Qn" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "mLocation" : "12.9758123 77.7578919",
      "post" : "test5",
      "timestamp" : 1464438695998
    },
    "-KIrMwNcSBR4ykm2n5ks" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760823 77.7576796",
      "post" : "test6",
      "timestamp" : 1464439130185
    },
    "-KIrNHQLY-PT07lEnFo1" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760823 77.7576796",
      "post" : "test 7😉",
      "timestamp" : 1464439220275
    },
    "-KIrOKW0wkX4Jjtp_6lM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760568 77.7577028",
      "post" : "test8",
      "timestamp" : 1464439495086
    },
    "-KIrQE8PWxVP7dsW4LUQ" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760223 77.7578286",
      "post" : "test9",
      "timestamp" : 1464439993419
    },
    "-KIrYsXtkfM6M2vClmCX" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.97617 77.7577447",
      "post" : "test 10",
      "timestamp" : 1464442260015
    },
    "-KIrdL0HJgyTLX6fDsjL" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9760616 77.7576665",
      "post" : "test11",
      "timestamp" : 1464443691516
    },
    "-KIv5c1OIISFBXXTeQF0" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9759527 77.7578142",
      "post" : "hello #sovan",
      "timestamp" : 1464501699050
    },
    "-KIwsU1TyXRV7PTYqfhv" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9755286 77.7579091",
      "post" : "test from sovan",
      "timestamp" : 1464531546607
    },
    "-KIwvMArad5wufME46d2" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9762825 77.7577278",
      "post" : "test2 from sovan",
      "timestamp" : 1464532300871
    },
    "-KIx9jBV7qqOhb_04FiR" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9761452 77.757783",
      "post" : "test3 from sovan",
      "timestamp" : 1464536331444
    },
    "-KIxATLiQRW6VPUulgLl" : {
      "author" : "-KIT9WWG--HaRzCm6fg_",
      "authorName" : "sovan",
      "mLocation" : "12.9762063 77.7577588",
      "post" : "test4 from sovan",
      "timestamp" : 1464536524673
    },
    "-KJ-aFZu75WE8fu4alTM" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9791569 77.7288152",
      "post" : "test12 from akshay",
      "timestamp" : 1464593877941
    },
    "-KJ132_Yz1lInWMM1bZc" : {
      "author" : "-KJ12Z9IZNz_kGNIcPs1",
      "authorName" : "newuser",
      "mLocation" : "12.9760395 77.7577474",
      "post" : "hum hai new user",
      "timestamp" : 1464618466004
    },
    "-KJ4SJGPOeh3OxkcVuZC" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9812033 77.7240957",
      "post" : "hi test 13",
      "timestamp" : 1464675421809
    },
    "-KJ6DbioHox2NSLR0Z8T" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9759283 77.7577924",
      "post" : "feeling #great #partying # ",
      "timestamp" : 1464705121602
    },
    "-KJ6EAoFUoGPOyduvkvf" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "mLocation" : "12.9759283 77.7577924",
      "post" : "#coding",
      "timestamp" : 1464705269345
    },
    "-KJ6J4ehY86MbFdL6mN-" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#you" ],
      "mLocation" : "12.9757097 77.7578994",
      "post" : "love making ##you",
      "timestamp" : 1464706555095
    },
    "-KJ6JJeQ3VZ-6B59Z7xx" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ null, "#a", "#b", "#c", "#d" ],
      "mLocation" : "12.9757097 77.7578994",
      "post" : "#a# b #b #c #d",
      "timestamp" : 1464706616439
    },
    "-KJ6Jqy4Q-DxJv9HCaaO" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#test1" ],
      "mLocation" : "12.9758581 77.7577817",
      "post" : "#test1",
      "timestamp" : 1464706756825
    },
    "-KJ6JxRZ5qczbKm-e_a2" : {
      "author" : "-KIT9PnIJbOGigKT1BHa",
      "authorName" : "akshay",
      "hashTags" : [ "#test2", "#test3" ],
      "mLocation" : "12.9758581 77.7577817",
      "post" : "#test2 #test3",
      "timestamp" : 1464706783346
    }
  },
  "user" : {
    "-KIT9PnIJbOGigKT1BHa" : {
      "email" : "imakshaybora@gmail.com",
      "friends" : {
        "-KIl0na9cZ1EY9jF4HSi" : "-KIa0kfOzm_hlUJ_pBbV",
        "-KIl18efwTfR1sq-6unv" : "-KIcHQ66Vig1aUjmWIfg",
        "-KIlN2cA8nSMU-0ljDgr" : "-KIT9WWG--HaRzCm6fg_",
        "-KIlOczOGj9oWt3itAoH" : "-KIa4-dHS7-iVT9b2Jh4",
        "-KIlOzMIiMIdFEfqdRWF" : "-KIYwF0kWZ97pkm0od-F",
        "-KIlPas-62dWJSGaWVd7" : "-KIa86JUxWbDnqStdDxh",
        "-KImcszKNBWW1O2BhZg6" : "-KIa9eEbHu4YNGnjpbZv"
      },
      "name" : "akshay",
      "password" : "alcohol",
      "uniqueId" : "-KIT9PnIJbOGigKT1BHa"
    },
    "-KIT9WWG--HaRzCm6fg_" : {
      "email" : "sovan.g@gmail.com",
      "friends" : {
        "-KIlN2eNvL2poRh2L1aG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "sovan",
      "password" : "sovan",
      "uniqueId" : "-KIT9WWG--HaRzCm6fg_"
    },
    "-KIYu5eUE04if17exaYe" : {
      "email" : "arnab.da@gmail.com",
      "name" : "arnab",
      "password" : "arnab",
      "uniqueId" : "-KIYu5eUE04if17exaYe"
    },
    "-KIYwF0kWZ97pkm0od-F" : {
      "email" : "a.k@g.com",
      "friends" : {
        "-KIlOzMIiMIdFEfqdRWG" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ardhendu ",
      "password" : "ak",
      "uniqueId" : "-KIYwF0kWZ97pkm0od-F"
    },
    "-KIa0kfOzm_hlUJ_pBbV" : {
      "email" : "ramesh.t@gmail.com",
      "friends" : {
        "-KIl0naLO88NGJZsIWzu" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "ramesh ",
      "password" : "ramesh",
      "uniqueId" : "-KIa0kfOzm_hlUJ_pBbV"
    },
    "-KIa4-dHS7-iVT9b2Jh4" : {
      "email" : "test@t.t",
      "friends" : {
        "-KIlOczeUi8zciZkvtSC" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "test",
      "password" : "test",
      "uniqueId" : "-KIa4-dHS7-iVT9b2Jh4"
    },
    "-KIa86JUxWbDnqStdDxh" : {
      "email" : "am@a.a",
      "friends" : {
        "-KIlPas9zzXvWLXdJva6" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "am",
      "password" : "am",
      "uniqueId" : "-KIa86JUxWbDnqStdDxh"
    },
    "-KIa9eEbHu4YNGnjpbZv" : {
      "email" : "aman@a.a",
      "friends" : {
        "-KImcszKNBWW1O2BhZg7" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "aman",
      "password" : "aman",
      "uniqueId" : "-KIa9eEbHu4YNGnjpbZv"
    },
    "-KIcHQ66Vig1aUjmWIfg" : {
      "email" : "Khusbu.a@g.com",
      "friends" : {
        "-KIl18egKD9k1xv8rhd5" : "-KIT9PnIJbOGigKT1BHa"
      },
      "name" : "Khusbu Agarwal ",
      "password" : "Khusbu",
      "uniqueId" : "-KIcHQ66Vig1aUjmWIfg"
    },
    "-KJ12Z9IZNz_kGNIcPs1" : {
      "email" : "newuser@gmail.com",
      "name" : "newuser",
      "password" : "newuser",
      "uniqueId" : "-KJ12Z9IZNz_kGNIcPs1"
    },
    "-KJEgt7DD125dBgwSPSh" : {
      "email" : "test1@user.com",
      "name" : "testuser",
      "password" : "test1",
      "uniqueId" : "-KJEgt7DD125dBgwSPSh"
    }
  }
}

Currently i am achieving this by the code below:

    postQuery=posts.orderByChild("timestamp");

    friends.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot friendSnap) {
            for(DataSnapshot snap:friendSnap.getChildren()) {
                friendStringList.add(snap.getValue(String.class));
            }
            postQuery.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot postSnap) {
                    postsTest.clear();
                    for (DataSnapshot snapshot : postSnap.getChildren()) {
                        if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                postsTest.add(snapshot.getValue(PostPOJO.class));
                        }
                    }
                    Collections.reverse(postsTest);
                    hide();
                    postAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Is it a good way to implement or can i use some queries to get things in a better way?

Please Guide.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Akshay Bora
  • 101
  • 8
  • With that query you will show all the posts not only the ones the user or his friend publishes...are all the posts supposed to be seen by anyone?? – Ymmanuel Jun 02 '16 at 19:36
  • No.. only user and their friends can see the post posted by the user in a reyclerview on their main page. Like facebok newsfeed – Akshay Bora Jun 02 '16 at 19:45
  • In "postQuery=posts.orderByChild("timestamp"); " if posts is the node with all your posts and in the query you only order by timestamp, with a ValueEventListener all the posts anyone publishes ordered by timestamp – Ymmanuel Jun 02 '16 at 19:52
  • with your current structure to get only your friends posts you should query using orderbychild('author') and equalTo($YOUR_FRIEND_KEY) and start a listener for each friend found in the user... still with the ValueEventListener you would receive all the posts that friend created ..so in the long run you would be loading thousands of posts for no reason, you can avoid that using limitTo ..and for the time stamps you could see how to use the priority in each node to get from most recent to oldest – Ymmanuel Jun 02 '16 at 19:56
  • You've included a picture of the JSON tree in your question. Please replace that with the (minimal) actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Jun 02 '16 at 20:00
  • I am storing my list of friend in a Arraylist and then checking by iterating over all post if the post's author is there is that Arraylist. If yes i am adding that post to another Post list. Is there anything in Firebase Query like IN in SQL, So that i can make a query like: post.orderByChildren("author").equalto(IN(FriendsList)). can't i optimize the code futher to get the results in the best way? – Akshay Bora Jun 03 '16 at 03:03
  • Any way by which we can use less no. Of listener to get the job done? @Puff – Akshay Bora Jun 03 '16 at 13:24
  • Looping and loading is not as expensive as you may thing. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Jun 04 '16 at 16:12
  • yes i am making request to data in parts now. as per required by the user. First fetching some data and then as the user scrolls i am requesting the next data set. – Akshay Bora Jun 04 '16 at 16:23
  • @FrankvanPuffelen i had posted my answer can you please check if you are talking something similar? Hoping that this will consume less data and take less time when thousands of posts are there.. – Akshay Bora Jun 04 '16 at 16:31

1 Answers1

2

I am now implementing the above in this way. If anyone is having a better way to implement please post. first checking 20 posts and then on scroll from the user fetching next data set.

postQuery=posts.orderByKey().startAt("").limitToLast(20);

        friends.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot friendSnap) {
                for(DataSnapshot snap:friendSnap.getChildren()) {
                    friendStringList.add(snap.getValue(String.class));
                }
                postQuery.addValueEventListener(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot postSnap) {
                        postsTest.clear();
                        int count=0;
                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                            if(count==0) {
                                start = snapshot.getKey();
                                count++;
                            }
                           // start=snapshot.getKey();
                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                    postsTest.add(snapshot.getValue(PostPOJO.class));
                            }
                        }
                        Collections.reverse(postsTest);
                        hide();
                        count=0;
                        postAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
            {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy)
                {
                    if(dy > 0) //check for scroll down
                    {
                        visibleItemCount = mLayoutManager.getChildCount();
                        totalItemCount = mLayoutManager.getItemCount();
                        pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                        if (loading)
                        {
                            if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                            {
                                loading = false;
                                Log.v("...", "Last Item Wow !");

                                //Do pagination.. i.e. fetch new data
                                show();
                                postQuery=posts.orderByKey().endAt(start).limitToLast(20);

                                postQuery.addValueEventListener(new ValueEventListener() {

                                    @Override
                                    public void onDataChange(DataSnapshot postSnap) {
                                        postsList.clear();
                                        int count=0;
                                        for (DataSnapshot snapshot : postSnap.getChildren()) {
                                            if(count==0) {
                                                start = snapshot.getKey();
                                                count++;
                                            }
                                            //start=snapshot.getKey();
                                            if (friendStringList.contains(snapshot.getValue(PostPOJO.class).getAuthor()) || (snapshot.getValue(PostPOJO.class).getAuthor().equals(uniqueId))) {
                                                postsList.add(snapshot.getValue(PostPOJO.class));
                                                loading=true;

                                            }
                                            else if(snapshot.getKey().equals(FirstPostKey)){
                                                loading=false;
                                                Snackbar.make(mView,"Reached End",Snackbar.LENGTH_LONG).show();
                                            }
                                        }
                                        postsList.remove(postsList.size()-1);
                                        Collections.reverse(postsList);
                                        postsTest.addAll(postsList);
                                        hide();
                                        count=0;
                                        postAdapter.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });


                            }
                        }
                    }
                }
            });
Akshay Bora
  • 101
  • 8