如何仅从Firebase获取新添加的数据? [英] How to get only newly added data from the Firebase?

查看:50
本文介绍了如何仅从Firebase获取新添加的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建聊天应用程序时,我正在使用Firebase.它运行完美,只有每次添加或删除新项目时,从Firebase获取整个数据列表时,我都会遇到一个问题.

As i am creating the chat app, for it i am using the Firebase. It works perfectly, only one problem which i am getting that each time i am getting whole list of data from Firebase, when new item is added or deleted into it.

我想要的是,只有新添加的记录应该来自Firebase,而不是整个数据列表.

What i want that , only newly added record should come from Firebase , not whole list of data.

请检查我下面的代码

 ArrayList<ChatMessage> MY_ARRAYLIST= new ArrayList<>();

        MsgViewHolder viewModel = ViewModelProviders.of(this).get(MsgViewHolder.class);

        LiveData<DataSnapshot> liveData = viewModel.getDataSnapshotLiveData();

        liveData.observe(this, dataSnapshot -> {
            if (dataSnapshot != null) {
                MY_ARRAYLIST.clear();  //  I NEED TO CLEAR THE ARRAY-LIST TO GET THE REFRESHED DATA..I DO NOT WANT TO LOAD WHOLE LIST ON EACH TIME
                if (dataSnapshot.exists())
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        ChatMessage bean = ds.getValue(ChatMessage.class);

                        assert bean != null;
                        MY_ARRAYLIST.add(bean);

                    }
            }
        });

就像上面的代码一样,我正在使用 MY_ARRAYLIST.clear()来清除数据以获取新添加的记录.每次从 DataSnapshot 中获取整个数据列表,无论何时添加或删除新记录.它们是从Firebase仅获取新添加的数据而不是整个 List 的任何方法吗?

As in above code i am using MY_ARRAYLIST.clear() to clear data, to get the newly added record.On each time from DataSnapshot , i am getting the whole list of data, whenever new record added or deleted.Is their any method to get only newly added data NOT whole List from Firebase?

推荐答案

您可以使用 addChildEventListener()给您 DatabaseReference ,当有任何回调方法时,它将通知您孩子被添加/删除/更新/删除或移动

You can use addChildEventListener() to you DatabaseReference which will notify you on different callback method when any child is add/removed/updated/deleted or moved

databaseRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            // If any child is added to the database reference
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            // If any child is updated/changed to the database reference
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
          // If any child is removed to the database reference
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
             // If any child is moved to the database reference
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            Log.e("FirebaseListAdapter", "Listen was cancelled, no more updates will occur");
        }
    });

每个回调中的DataSnapshot将为您提供子项的信息

The DataSnapshot in each callback will provide you the information of the child

这篇关于如何仅从Firebase获取新添加的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆