In my android app, I am using Firebase to store some user information.
I am storing user's username
in 3 different places.
I have this option in my app where I allow users to update their username
. I want to know the best reliable way of updating the username
in all those 3 places.
My approach is to do this:
-- Update in place 1. If that was successful, then update in place 2. If that was successful, then update in place 3. If that was successful, then I am all good. username
got updated successfully in all 3 places.
But how would I handle a scenario like this:
-- Attempt to update in place 1. Let's say that was successful, then I attempt to update in place 2. Let's say that was successful too, then I attempt to update in place 3. Let's say that it failed to update in place 3. At this point, I have updated username
in place 1 and place 2, but not in place 3.
That scenario can happen if user's phone disconnects from Wifi while Firebase is trying to update. And I want to handle it in a proper way if that happens. Below is some reference code on how I am doing this:
placeOneReference.child(key).setValue(newUsername)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
placeTwoReference.child(key).setValue(newUsername)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
placeThreeReference.child(key).setValue(newUsername)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// Everything is successful
}
}
});
}
}
});
}
});