iOS快速解析+如何检查数据库中是否已存在电子邮件 [英] iOS swift parse + how to check if email already exist in database

查看:110
本文介绍了iOS快速解析+如何检查数据库中是否已存在电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Parse进行iOS Swift项目.我需要允许用户更新他们的电子邮件,但是它应该是唯一的.目前,我的代码如下:

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

我认为这不是正确的代码,并且也不起作用.有人可以指导我如何适应这个问题,如果可以防止两个PFQuery和findObjectsInBackgroundWithBlock出现,我认为这是必需的.一种是检查当前数据库中是否存在该电子邮件,另一种是更新行.

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.

推荐答案

如果您尝试设置PFUser的电子邮件字段,则Parse会自动检测到这一点.例如,当向用户注册您的应用程序时,Parse将返回一个错误,表明该电子邮件已被使用,并且不允许邮件合并.实际上,我敢肯定,它甚至可以为您提供警报.

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.

如果用户尝试更新电子邮件,则在应用程序的任何其他部分中,Parse都将以相同的方式工作,即使没有错误提示,您也必须加以解决.

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.

因此,您无需执行任何查询或任何操作.您只需尝试更新PFUser对象的电子邮件字段并保存它,如果这样的电子邮件地址已经存在,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. 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
       }
 }

这篇关于iOS快速解析+如何检查数据库中是否已存在电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆