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.