如何从firebase数据库中仅获取更新的子数据?

时间:2017-08-15 11:22:51

标签: android firebase firebase-realtime-database key-value firebase-storage

我可以从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

1 个答案:

答案 0 :(得分:0)

如果您想知道哪些特定子节点已被修改,您应该使用Firebase的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确切地告诉您发生了什么:onChildAddedonChildRemovedonChildChangedonChildMoved

Btw:您使用的Firebase SDK至少有一年半的历史,之后一直没有更新过。我高度* 建议您切换到Firebase SDK的新版本。如果您要转换现有代码,请查看upgrade guide