An onComplete
listener fires when the state you set has been accomplished on the server. There is no way to change this behavior of the onComplete
listener.
If you want to ensure that only a single user can remove a value, and that you know it is the current user, you should use a transaction. With a transaction you determine the new value of a node, based on its current value.
ROOT_REF.child("111").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Object value = mutableData.getValue();
if (value == null) {
// Node doesn't exist
return Transaction.success(mutableData);
}
// Remove value and report transaction success
mutableData.setValue(null);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b,
DataSnapshot dataSnapshot) {
// Transaction completed
Log.d(TAG, "postTransaction:onComplete:" + databaseError);
}
});
Note that:
- A transaction's
doTransaction
method may fire multiple time, precisely when another user has modified the value already. For more on this, see Firebase runTransaction not working - MutableData is null.
- Since transactions read the current value of the node, they only work when the user is online.