使用Parse SDK和swift,PFUser.currentUser()。fetch()无效

时间:2015-12-18 17:07:57

标签: swift parse-platform

用户登录后如果他们后来决定要更改用户名,他们就有能力这样做。当我保存记录时,我发现用户名的解析错误已经存在,并且电子邮件已经存在。

我遇到的问题是,如果用户决定不更改他们的信息,那么应用程序将“BAD用户名”保留在缓存中并在应用程序上显示它。

我试过调用PFUser.currentUser()。fetch(),如下所示,从数据库中的内容中刷新它,但无济于事。

唯一有效的方法是退出并重新登录,以便将其恢复到数据库中的最新状态。

有什么想法吗?

继承我的代码以捕获用户名错误并尝试刷新PFUser

func saveUser() {

        let username = nameField.text
        let firstname = firstNameField.text! as String
        let lastname = lastNameField.text! as String
        let profilemsg = profileMessage.text! as String
        let email = emailField.text! as String

        if username!.characters.count > 0 {
            let user = PFUser.currentUser()
            if let user = user {
                user["username"] = username
                user["first"] = firstname
                user["last"] = lastname
                user["greeting"] = profilemsg
                user["email"] = email

            }
            user!.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
                if (success) {
                    ProgressHUD.showSuccess("Saved")
                } else {
                    let errorCode = error!.code

                    switch errorCode {

                    case 202:
                        ProgressHUD.showError("Username " + username! + " already taken")
                        self.nameField.text = nil
                        do
                        {
                            try PFUser.currentUser()!.fetch()
                            print("User refreshed")
                        }
                        catch{

                        }
                        break

                    case 203:
                        ProgressHUD.showError("E-mail " + email + " already taken")
                        self.emailField.text = nil
                        do
                        {
                            try PFUser.currentUser()!.fetch()
                            print("User refreshed")
                        }
                        catch{

                        }
                        break
                    default:
                        ProgressHUD.showError("Network Error")
                        break
                    }
                }
            }
        } else {
            ProgressHUD.showError("Userame field must not be empty")
        }
    }

1 个答案:

答案 0 :(得分:0)

无需创建新的用户实例来保存当前用户。

只需对currentUser进行更改即可:

PFUser.currentUser()!["username"] = username
...

然后保存:

PFUser.currentUser()!.saveInBackgroundWithBlock {(...)}