1

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
                                                }
                                            }
                                        });
                            }
                        }
                    });
        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Parth Bhoiwala
  • 1,282
  • 3
  • 19
  • 44
  • 1
    This sounds very similar to [How to write denormalized data in Firebase ](https://stackoverflow.com/questions/30693785/how-to-write-denormalized-data-in-firebase). Levi's answer is the top recommendation in my answer there. – Frank van Puffelen Apr 05 '18 at 13:10

1 Answers1

1

You should use multipath update in this case. This way you don't cascade the requests, but rather let firebase handle them in one "atomic" update function.

    Map<String, Object> value = new HashMap<>();
    value.put("username", username);

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/path1/" + key, value);
    childUpdates.put("/path2/" + key, value);
    childUpdates.put("/path3/" + key, value);



    mDatabase.updateChildren(childUpdates);

You can even add a onComplete listener to update children if you want to be notified when it is done or if anything else (an error) happens.

mDatabase.updateChildren(childUpdates, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

        }
    });
Levi Moreira
  • 11,917
  • 4
  • 32
  • 46