Search code examples
iosswiftparse-platformpfuser

Add column to PFUser AFTER signup? Parse, Swift


I would like my user to add/edit details about their profile after they register with my app.

@IBAction func doneEditting(sender: AnyObject) {
    self.completeEdit()
}

func completeEdit() {
    var user = PFUser()
    user["location"] = locationTextField.text

    user.saveInBackgroundWithBlock {
        (succeeded: Bool, error: NSError?) -> Void in
        if let error = error {
            let errorString = error.userInfo?["error"] as? NSString
            println("failed")
        } else {
            self.performSegueWithIdentifier("Editted", sender: nil)
        }
    }
}

the breakpoint stops right at user.saveInBackgroundWithBlock. No of the docs show how to append new columns after the signup.

Thanks!


Solution

  • You are mentioning that the user should be able to edit their profile after they have registered. When registering a user with Parse using signUpInBackgroundWithBlock, then the Parse SDK will automatically create a PFUser for you.

    In your provided code you are creating and saving a completely new PFUser instead of getting the one which is currently logged in. If you are not using the PFUser which is logged in, then you will get the following error at user.saveInBackgroundWithBlock (which you are also mentioning in your post):

    User cannot be saved unless they are already signed up. Call signUp first
    

    To fix this, you will need to change:

    var user = PFUser()
    

    To the following:

    var user = PFUser.currentUser()!
    

    The rest of your code (for example user["location"] = locationTextField.text) works fine and will dynamically/lazily add a new column to your User database (which is what you want).