0

I'm working on iOS Swift project with Parse. I need to allow users to update their email but it should be unique. Currently, my code looks like following:

var user = PFUser.currentUser()
    var userName = user.username

    var profQuery = PFQuery(className: "User")
    profQuery.whereKey("email", equalTo: fnEditEmail)

    profQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil && objects!.count < 1 {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    println(object.objectId)
                    object.setValue(self.fnEditEmail.text, forKey: "email")
                    object.setValue(self.fnEditAge.text, forKey: "age")
                    object.setValue(self.fnEditGender.text, forKey: "gender")
                    object.setValue(self.fnEditText.text, forKey: "fullname")

                    object.setValue(self.keyWord1.text, forKey: "key1")
                    object.setValue(self.keyWord2.text, forKey: "key2")
                    object.setValue(self.keyWord3.text, forKey: "key3")
                    object.setValue(self.keyWord4.text, forKey: "key4")

                    object.saveInBackgroundWithBlock {
                        (succeeded: Bool!, error: NSError!) -> Void in
                        if error == nil {
                             println "Profile Updated."
                            } else {
                                println "Failed"
                            }
                    }
                }
            } else if error == nil && objects!.count >= 1 {
                println "email already exist."
                } else if error != nil {
                    println "couldn't update, please try again."
                    }
            }
        }

I don't think this is correct code and it's not working either. Could somebody please guide me how can I fit this and also, if I can prevent two PFQuery and findObjectsInBackgroundWithBlock, which I think what is required here; One to check if that email exists in current database and one to update the row.

pbush25
  • 5,228
  • 2
  • 26
  • 35
Guru
  • 409
  • 3
  • 11

1 Answers1

4

Parse automatically detects this if you try to set the email field of a PFUser. For instance, when signing a user up to your application, Parse will return an error that the email is already being used and won't allow singup. In fact, it even presents the alert for you I'm pretty sure.

In any other part of your app if the user is trying to update their email, Parse will work the same way, albeit without the error presentation which you would have to take care of.

Because of this, you don't have to do any queries or anything. You would simply try to update the email field of a PFUser object, and save it, and Parse will return the error for you if such an email address already exists.

Bottom line is Parse will never allow non-unique email addresses for any PFUser, so you don't have to write code to worry about this. Just worry about email address validation if that's something you want, and then worry about presenting an alert if Parse returns an error.

var user = PFUser.currentUser()
user.setValue(newEmail, forKey: "Email")
user.saveInBackgroundWithBlock {
  (succeeded: Bool!, error: NSError!) -> Void in
       if error == nil {
           println "Profile Updated."
       } else {
           println "Failed"
           //present alert to user to let them know that it failed
           //ask them to try a new email address
       }
 }
pbush25
  • 5,228
  • 2
  • 26
  • 35
  • Hey, thanks, you're right. I'm not required to write separate code to check if the email already exists. Parse took care of it. (Y) – Guru Jun 25 '15 at 09:35