简体   繁体   中英

Firebase Realtime Database: Is there a way to avoid calling onDataChange when a value is removed?

I'm wondering if there is a way to avoid calling onDataChange when removing a value. I want to remove all values inside a node but the node cannot be removed completely because I set this inside onDataChange .

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
     if (!(dataSnapshot.haschild(aValue))
           //add aValue to the child

     if (dataSnapshot...

So the node is recreated immediately after the node is removed. Also, the statement inside onDataChange is called even when the node is already removed so the app crashes. Currently, I set boolean to check if the node is removed and it works but I'm wondering if there's a better way to do it.

I'm wondering if there is a way to avoid calling onDataChange when removing a value.

No, there is not. According to the official documentation regarding ValueEventListener's onDataChange() method:

This method will be called with a snapshot of the data at this location. It will also be called each time that data changes.

With other words, even if it is a write, an update or even a delete operation, this method is triggered. So everytime a change in your database takes place, onDataChange() fires and unfortunately you cannot avoid this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM