0

我能夠從firebase數據庫中獲取所有子節點。如何僅從Firebase數據庫獲取更新的子數據?

Firebase firebase = new Firebase("https://myfiebase-url.firebaseio.com/"); 
    firebase.addValueEventListener(new com.firebase.client.ValueEventListener() { 
     @Override 
     public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) { 
      System.out.println(dataSnapshot.getValue()); 
      Log.e("testing message", dataSnapshot.getValue() + "--->") 
      } 


     @Override 
     public void onCancelled(FirebaseError firebaseError) { 

     } 

enter image description here

但我的問題是,當任何新數據中的節點添加,例如,如果一個或多個數據的內部節點2 加入當時onDataChange()回報的全部數據。我只想檢索更新的數據。

現在我得到這樣的迴應:

E/testing message: [null, {-KrBu6CM_IP7xwQHcYOT={date=10-08-2017, stared=, chatId=, time=Thu Aug 10 21:39:32 GMT+05:30 2017, message=Gagsgs, userId=0, timeStamp=}, -KrXiYBOUqeZ7BtzF9jw={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 03:20:38 IST 2017, message=Fggffg, userId=0, timeStamp=}, -KquvlHuja1PZ-hoW-Xf={date=07-08-2017, stared=, chatId=, time=Mon Aug 07 09:53:35 GMT+05:30 2017, message=Xvxcggg, userId=0, timeStamp=}, -KrXb8scQ3blRzt3HzUX={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:48:20 GMT+05:30 2017, message=Tggggg, userId=0, timeStamp=}, -KrXatbjGvt8HlZetAHN={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:47:13 GMT+05:30 2017, message=Gggfy, userId=0, timeStamp=}, -KrXaZINL9_ZEAMbVE_1={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:45:46 GMT+05:30 2017, message=Shubhank, userId=0, timeStamp=}, -KrXbBgEhJA3CMlHlTtX={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:48:31 GMT+05:30 2017, message=Tyyggg, userId=0, timeStamp=}, -KrXfVrqlVw8OXPOzjlw={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 03:07:22 GMT+05:30 2017, message=Fggh, userId=0, timeStamp=}, -KrXaV5zkbz369g4n2_N={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:45:28 GMT+05:30 2017, message=Gghhu, userId=0, timeStamp=}, -KrXb2rZgm98Jiu8dZ7I={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:47:55 GMT+05:30 2017, message=Ytttt, userId=0, timeStamp=}, -KrXb1Mkp1pir5PekCGp={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:47:49 GMT+05:30 2017, message=Ttttt, userId=0, timeStamp=}, -KrXacX7z-InOYNLa6zB={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:46:03 GMT+05:30 2017, message=Eeqdss, userId=0, timeStamp=}, -KrXagbxXVN5dx1YkPBD={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:46:20 GMT+05:30 2017, message=Mn mm, userId=0, timeStamp=}, -KrXcbNyUQo8ia7WRoO1={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:54:43 IST 2017, message=Bdhdhdd, userId=0, timeStamp=}, -KrXcZLhA5RVJceDPai6={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:54:30 IST 2017, message=Xdff, userId=0, timeStamp=}, -KrXagsuQ2iLmJVhbnoi={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:46:21 GMT+05:30 2017, message=Lp, userId=0, timeStamp=}, -KrXhz1XeWxYguDNt8Yv={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 03:18:10 GMT+05:30 2017, message=Fhfdt, userId=0, timeStamp=}, -KrXanJdSh9iPqCJCUT0={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:46:47 GMT+05:30 2017, message=Ghjhh, userId=0, timeStamp=}, -KrXb-jZqlwOEbUswppw={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:47:42 GMT+05:30 2017, message=Dfgffg, userId=0, timeStamp=}, -KrXeHxow-Jb5n0LuW5s={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 03:02:03 IST 2017, message=Ftffft, userId=0, timeStamp=}, -KrXcV2Og5InEEtDacMj={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:54:13 IST 2017, message=Gfggg, userId=0, timeStamp=}, -KrXb6zFyP5DnaexkMmU={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:48:12 GMT+05:30 2017, message=Tttt, userId=0, timeStamp=}, -KrXc4bhHWZsQfbVj8HU={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:52:25 IST 2017, message=Vhhhh, userId=0, timeStamp=}, -KquvqzMKh3eNEnIzhhr={date=07-08-2017, stared=, chatId=, time=Mon Aug 07 09:53:59 GMT+05:30 2017, message=Gdgsggdd, userId=0, timeStamp=}, -KrXayJVxlC3hXqSiVnF={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:47:32 GMT+05:30 2017, message=Frsddr, userId=0, timeStamp=}, -KqqdBq5Ui8Ij-jg5kN2={date=06-08-2017, stared=, chatId=, time=Sun Aug 06 13:53:59 GMT+05:30 2017, message=Gfggg, userId=0, timeStamp=}}, {-KrXSl1kSbbQF38mHXtb={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:07:18 GMT+05:30 2017, message=Gggg, userId=0, timeStamp=}, -KrX6yitdWGiBF4SR2MJ={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 00:32:08 GMT+05:30 2017, message=Gggg, userId=0, timeStamp=}, -KrXTEKQpj1Sz7qUkgRp={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 02:09:23 GMT+05:30 2017, message=Cffg, userId=0, timeStamp=}}, {-KrXLO1INF4-0HewRVD7={date=15-08-2017, stared=, chatId=, time=Tue Aug 15 01:35:05 GMT+05:30 2017, message=Aastha, userId=0, timeS 

回答

0

如果你想獲得告訴特定的子節點已經被修改了什麼,你應該火力地堡的ChildEventListener使用:

firebase.addChildEventListener(new com.firebase.client.ChildEventListener() { 
    @Override 
    public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String previousChildKey) { 
     Log.i("onChildAdded", "Child "+dataSnapshot.getKey()+" was added after "+previousChildKey) 
    } 

    @Override 
    public void onChildRemoved(com.firebase.client.DataSnapshot dataSnapshot) { 
     Log.i("onChildRemoved", "Child "+dataSnapshot.getKey()+" was removed") 
    } 
    ... 

onChild* methods確切地告訴你發生了什麼事情:onChildAdded,onChildRemoved,onChildChangedonChildMoved

順便說一句:您使用的Firebase SDK至少有一年半的時間,並且自此以後沒有更新過。 I 高度*建議您切換到Firebase SDK的新版本。如果您想要轉換現有代碼,請查看upgrade guide

+0

但實際上我想檢測新節點是否在子子項下添加。假設節點在4或3內部添加。 –

+0

無法在Firebase數據庫SDK中偵聽'onGrandchild *'事件。你將不得不改變你的數據模型來適應你的用例。 –

+0

謝謝,弗蘭克...有什麼辦法可以使用'addListenerForSingleValueEvent'來獲取多個節點的數據 –

相關問題