2016-06-02 5 views
2

Я хочу заполнить все сообщения пользователя и их друзей. Моя реализация базы данных прилагаетсяFirebase видеть сообщения пользователя

Реализация Пользователь User implementation

Сообщений Реализация Posts Implementation

Текст 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" : "[email protected]", 
     "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" : "[email protected]", 
     "friends" : { 
     "-KIlN2eNvL2poRh2L1aG" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "sovan", 
     "password" : "sovan", 
     "uniqueId" : "-KIT9WWG--HaRzCm6fg_" 
    }, 
    "-KIYu5eUE04if17exaYe" : { 
     "email" : "[email protected]", 
     "name" : "arnab", 
     "password" : "arnab", 
     "uniqueId" : "-KIYu5eUE04if17exaYe" 
    }, 
    "-KIYwF0kWZ97pkm0od-F" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KIlOzMIiMIdFEfqdRWG" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "ardhendu ", 
     "password" : "ak", 
     "uniqueId" : "-KIYwF0kWZ97pkm0od-F" 
    }, 
    "-KIa0kfOzm_hlUJ_pBbV" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KIl0naLO88NGJZsIWzu" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "ramesh ", 
     "password" : "ramesh", 
     "uniqueId" : "-KIa0kfOzm_hlUJ_pBbV" 
    }, 
    "-KIa4-dHS7-iVT9b2Jh4" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KIlOczeUi8zciZkvtSC" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "test", 
     "password" : "test", 
     "uniqueId" : "-KIa4-dHS7-iVT9b2Jh4" 
    }, 
    "-KIa86JUxWbDnqStdDxh" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KIlPas9zzXvWLXdJva6" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "am", 
     "password" : "am", 
     "uniqueId" : "-KIa86JUxWbDnqStdDxh" 
    }, 
    "-KIa9eEbHu4YNGnjpbZv" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KImcszKNBWW1O2BhZg7" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "aman", 
     "password" : "aman", 
     "uniqueId" : "-KIa9eEbHu4YNGnjpbZv" 
    }, 
    "-KIcHQ66Vig1aUjmWIfg" : { 
     "email" : "[email protected]", 
     "friends" : { 
     "-KIl18egKD9k1xv8rhd5" : "-KIT9PnIJbOGigKT1BHa" 
     }, 
     "name" : "Khusbu Agarwal ", 
     "password" : "Khusbu", 
     "uniqueId" : "-KIcHQ66Vig1aUjmWIfg" 
    }, 
    "-KJ12Z9IZNz_kGNIcPs1" : { 
     "email" : "[email protected]", 
     "name" : "newuser", 
     "password" : "newuser", 
     "uniqueId" : "-KJ12Z9IZNz_kGNIcPs1" 
    }, 
    "-KJEgt7DD125dBgwSPSh" : { 
     "email" : "[email protected]", 
     "name" : "testuser", 
     "password" : "test1", 
     "uniqueId" : "-KJEgt7DD125dBgwSPSh" 
    } 
    } 
} 

В настоящее время я достижения этой цели с помощью кода ниже:

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) { 

     } 
    }); 

Является ли это хорошим способом реализации или могу ли я использовать некоторые запросы, чтобы улучшить ситуацию?

Руководство по эксплуатации.

+0

С этим запросом вы покажете все посты не только тем, которые публикует пользователь или его друг ... все сообщения, которые должны быть замечены кем-либо? – Ymmanuel

+0

Нет. Только пользователь и их друзья могут видеть сообщение, отправленное пользователем в reyclerview на главной странице. Как facebok newsfeed –

+0

В "postQuery = posts.orderByChild (" timestamp ");" если сообщения - это узел со всеми вашими сообщениями и в запросе вы заказываете только по timestamp, с ValueEventListener все сообщения, публикуемые по заказу timestamp – Ymmanuel

ответ

2

Теперь я реализую вышеизложенное. Если у кого-то есть лучший способ реализовать, напишите. сначала проверяя 20 сообщений, а затем прокручиваем от пользователя выборку следующего набора данных.

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) { 

            } 
           }); 


          } 
         } 
        } 
       } 
      }); 
+0

Благодарим вас за отличный ответ –

Смежные вопросы