0

I have the following code that allows users to update their data:

String username = "Any username that is currently already used by another user";

ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
    currentUser.setUsername(username);

    currentUser.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                // All good!
            } else {
                // Error: Account already exists for this username.
                Log.e("Error: ", e.getMessage());
            }
        }
    }
});

When the user is trying to update the username with one that is already used by someone else, it throws an error:

Account already exists for this username.

Which is exactly what I'd want it to, but when the user goes back (without making another request to change the username to one that is available) the ParseUser.get("username") returns the value that wasn't saved because it already exists (instead of the real value stored at the moment in the server).

ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.fetchInBackground(new GetCallback<ParseUser>() {
    public void done(ParseUser user, ParseException exception) {
        if (exception == null) {
            // username now returns ("Any username that is currently already used by another user").
            String username = user.get("username").toString().trim();
        } else {
            // Error
        }
    }
});

The only way I've found so far to fix the issue is by uninstalling/reinstalling the app. What's causing this? And is there any good way to fix this?

Tom Fox
  • 897
  • 3
  • 14
  • 34
Alaa Salah
  • 1,047
  • 3
  • 12
  • 28

1 Answers1

0

You should backup the current username before setUsername(newName). if exception happened you have two options either :

  • Re fetch the user object by ParseUser.getCurrentUser().fetch() (to restore the object to his previous state)
  • Re set the username to the previous value(that you already back up before)

Why this happened ? because when you called the setUsername() of the user object you changed your local copy of the object (but you didn't sync it yet with the server) and then when you made save() and the save operation failed the local copy still has the latest changes that you made locally

Bahaa KA
  • 157
  • 1
  • 12