2

I am new to Firebase Real-time Database and don't have an idea that how to fetch only the recent updated object from the table.

I have tried using "ChildEventListener" but when I initialize the Listener for the first time it fetches the last row from the database. It should not be fetched if it was not updated. I want object only when it is updated or newly added. I have done this,

databaseReference.child("last_chat").orderByChild("s_id").equalTo(preference.getUserData().getId()).limitToLast(1).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.e("dataSnapShot", dataSnapshot.toString() + "      " + s);
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e("dataSnapShotChagned", dataSnapshot.toString() + "      " + s);

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            Log.e("dataSnapShotRemoved", dataSnapshot.toString());

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("onCalcnelle", databaseError.toString());
        }
    });

Thank you in advance.

Mehul_MG
  • 68
  • 7
  • See https://stackoverflow.com/questions/33885059/how-to-only-get-new-data-without-existing-data-from-a-firebase, https://stackoverflow.com/questions/43440908/firebase-child-added-for-new-items-only – Frank van Puffelen Jan 01 '18 at 16:26
  • As i saw the answers, they all are fetching whole data first time , But i don't want that, i just want to get only the last updated object from Db.@FrankvanPuffelen – Mehul_MG Jan 02 '18 at 04:03
  • Please post your solution as an answer and not in the question. – Frank van Puffelen Jan 02 '18 at 04:50

2 Answers2

1

I have solved the issue.

I just need to put my code inside the OnChildChanged instead of putting my code in OnChildAdded.

Log dataSnapShot shown when first running the application, whereas as soon as I change or update object dataSnapShotChagned is shown.

Thus it worked for me, OnChildAdded method is called every time while initializing the listener where OnChildChanged method is called only when there is a change in a particular object.

Thanks a lot all for the help.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Mehul_MG
  • 68
  • 7
0
public ValueEventListener addValueEventListener (ValueEventListener listener)

Also: Google Play services Add a listener for changes in the data at this location. Each time time the data changes, your listener will be called with an immutable snapshot of the data.

Firebase Doc

Hope this helps.

Chintan Joshi
  • 1,207
  • 1
  • 9
  • 17
  • Thank you @Chin10. in this snapshot, only the updated data will be shown? – Mehul_MG Jan 01 '18 at 12:15
  • It also shows the record when i first time run the application. I want to get Data only when there is update or new data added. – Mehul_MG Jan 01 '18 at 12:19
  • As you asked for @Mehul_MG, It will surely provide you updated data. However, you will have to handle data in way you want to display them. – Chintan Joshi Jan 01 '18 at 12:20
  • valueeventlistener is the same as childeventlistener(they both retrieve data), they have some differences but it does not really matter in this question.. – Peter Haddad Jan 01 '18 at 13:20
  • Yes i have searched for both this listeners, i also implemented the ValueEventListener as well. But the thing is i want only the updated data, this gives me all the object data in snapshot for the first time.@Peter – Mehul_MG Jan 02 '18 at 04:05