I am incrementing multiple nodes' value using ServerValue.increment() method
In below DB I want to increment Wallet/uID/bal value for multiple UIDs:
OnComplete callback always runs, but it doesn't always increments balance. I want to retry if values were not incremented, how can I know if values were incremented or not and how to retry?
Below is the code:
Map<String, Object> children = new HashMap<>();
children.put("Wallet" "/" UID1 "/bal", ServerValue.increment(10));
children.put("Wallet" "/" UID2 "/bal", ServerValue.increment(10));
children.put("Wallet" "/" UID3 "/bal", ServerValue.increment(10));
//and so on
FirebaseDatabase.getInstance().getReference().updateChildren(children, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
// how to know here? if successful or any error?
}
});
CodePudding user response:
All operations in a single call to updateChildren
either succeed or they all fail.
If they fail on the server, the error
parameter of your onComplete
handler will indicate why it failed; typically that'll be related to the security rules of your database.
CodePudding user response:
Why don't you try to check if the error is null or not? If the error is null, it's successful. Else fail. Try this code:
FirebaseDatabase.getInstance().getReference().updateChildren(children, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(@Nullable DatabaseError error, @NonNull DatabaseReference ref) {
if(error == null && ref != null){
// there was no error and the value is modified
}
else{
// there was an error. try to update again
}
}
});