learningthings
learningthings

Reputation: 73

Updating PFUser.currentUser

For my app the user can change their current username. So using parse and I set the new username for the current user shown in my code below. I then call saveInBackgroundWithBlock to save the new username. However I get this error "Username already taken." But now PFUser.currentUser.username is set to the invalid new username even though the save failed. I called fetch and that didn't change anything. How can I update the PFUser.currentUser?

    let user = PFUser.currentUser()
    user.username = newUsernameText
    user.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            self.helper.successAlert("Saved!", msg: "Your profile has been updated.")

        } else {
            // There was a problem, check error.description
            self.helper.errorAlert("Error", msg: (error?.localizedDescription)!)
            PFUser.currentUser().fetch()
            self.user = PFUser.currentUser()
            print(user.username) //prints the new username that was invalid

        }
    }

Upvotes: 0

Views: 622

Answers (1)

Stefanos Angelos
Stefanos Angelos

Reputation: 115

Try with changing the username to something totally else. There's a chance you already have the username in your database. Another way of going about this would be to query all existing users first and then to check if the username if taken or not with an if-else statement. If it's not update it, if it is throw an alert. Cheers

Upvotes: 1

Related Questions