'how to know if updateChildren() on Firebase Realtime database was successful or not?
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?
}
});
Solution 1:[1]
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.
Solution 2:[2]
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
}
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
| Solution 2 | Sambhav. K |

