简体   繁体   English

iOS快速解析+如何检查数据库中是否已存在电子邮件

[英]iOS swift parse + how to check if email already exist in database

I'm working on iOS Swift project with Parse. 我正在使用Parse进行iOS Swift项目。 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; 有人可以指导我如何安装它,如果可以防止两个PFQuery和findObjectsInBackgroundWithBlock出现,我认为这是必需的; One to check if that email exists in current database and one to update the row. 一种检查当前数据库中是否存在该电子邮件,另一种更新该行。

Parse automatically detects this if you try to set the email field of a PFUser . 如果您尝试设置PFUser的电子邮件字段,则Parse会自动检测到这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. 例如,当向用户注册您的应用程序时,Parse将返回一个错误,表明该电子邮件已被使用,并且不允许进行合并。 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. 如果用户尝试更新电子邮件,则在应用程序的其他任何部分中,Parse都将以相同的方式工作,即使没有错误提示,您也必须加以解决。

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. 您只需尝试更新PFUser对象的电子邮件字段并保存它,如果这样的电子邮件地址已经存在,Parse将为您返回错误。

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. 底线是Parse绝不允许任何PFUser使用非唯一的电子邮件地址,因此您不必编写代码来担心这一点。 Just worry about email address validation if that's something you want, and then worry about presenting an alert if Parse returns an error. 只需要担心电子邮件地址验证(如果需要),然后担心如果Parse返回错误会显示警报。

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
       }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM