nikenzo
nikenzo

Reputation: 13

How to not trigger childEventListener for firebase realtime database on a device when setting a value on same device?

I am currently having some issues with firebase realtime database.

When I e.g. delete an entry from the realtime database like so myRef.child(ID).setValue(null) the childEventListener

val firebaseItemsListener = object : ChildEventListener {
        override fun onChildAdded(dataSnapshot: DataSnapshot, previousChildName: String?) {
            Log.i("onChildAdded",dataSnapshot.toString())
        }
        override fun onChildChanged(dataSnapshot: DataSnapshot, previousChildName: String?) {
            Log.i("onChildChanged",dataSnapshot.toString())
        }
        override fun onChildRemoved(dataSnapshot: DataSnapshot) {
            Log.i("onChildRemoved",dataSnapshot.toString())
        }
        override fun onChildMoved(dataSnapshot: DataSnapshot, previousChildName: String?) {
            Log.i("onChildMoved",dataSnapshot.toString())
        }
        override fun onCancelled(databaseError: DatabaseError) {
            Log.i("onCancelled",databaseError.toString())
        }
    }
    myRef.addChildEventListener(firebaseItemsListener)

will be notified, more specificly the onChildRemoved part in this case. Now when I swipe away and delete an entry from a recyclerview on my phone I know that that entry is already deleted and I don't want my phone specifically to be notified because my phone doesn't need that information but firebase will trigger the onChildRemoved part anyway and inform my device about that change, which I don't want to happen. My question now is if there is an intended way to do this by firebase themselfs or some custom way I have to come up with? Thx in advance!

Upvotes: 0

Views: 45

Answers (1)

Data overflow
Data overflow

Reputation: 54

When using ChildEventListener, you can't control if this onChildRemoved() will trigger or not.

What you can do is ignore the callback and do nothing in the method.

    override fun onChildRemoved(dataSnapshot: DataSnapshot) {

     //do nothing here, when notified of a deleted child

    }

Upvotes: 1

Related Questions