【问题标题】:How to get only the updated child data from firebase database?如何从 firebase 数据库中仅获取更新的子数据?
【发布时间】:2017-08-15 11:22:51
【问题描述】:

我能够从 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) {

        }

但我的问题是,当在节点内添加任何新数据时,例如,如果在节点 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

【问题讨论】:

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


    【解决方案1】:

    如果你想知道哪个特定的子节点被修改了,你应该使用 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

    顺便说一句:您使用的 Firebase SDK 至少已有一年半的历史,此后一直没有更新。我强烈*建议您切换到Firebase SDK 的新版本。如果您要转换现有代码,请查看upgrade guide

    【讨论】:

    • 但实际上我想检测是否在子子项下添加了新节点。假设如果节点添加在 4 或 3 内。
    • 无法在 Firebase 数据库 SDK 中侦听 onGrandchild* 事件。您必须更改数据模型以适应您的用例。
    • 谢谢,弗兰克...有任何方法可以使用“addListenerForSingleValueEvent”获取多个节点的数据
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2020-09-04
    • 1970-01-01
    相关资源
    最近更新 更多