保存新用户Parse Swift时崩溃

时间:2015-12-11 01:51:14

标签: ios swift parse-platform

因此,当用户登录/注册时,系统会提示他们输入名称并保存。当他们按下保存时,新信息将被发送到解析并存储。我的问题是,如果我注册一个用户给他们一个名字,保存和注销,然后唱一个新用户,给他们一个名字,然后按保存,应用程序崩溃。然后我可以重新运行应用程序并再次保存它们的名称,它可以正常工作。

我设置了一个异常断点,它会中断:user.saveInBackground() 这是我保存按钮的代码

更新:我刚刚了解到,无论何时更改用户名并保存然后注销,然后使用其他用户登录并为其保存新名称,它就会崩溃。

import UIKit
import Parse

class ProfileViewController: UIViewController, UITextFieldDelegate, UINavigationBarDelegate {

var activityIndicator = UIActivityIndicatorView()

func alertDismiss(title: String, message: String) {
    var alertDismiss = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alertDismiss.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
        self.dismissViewControllerAnimated(true, completion: nil)
    }))
    self.presentViewController(alertDismiss, animated: true, completion: nil)
}

var user: PFUser? = PFUser.currentUser()

@IBOutlet weak var locationServiceSwitch: UISwitch!
@IBOutlet weak var profileNavigationBar: UINavigationBar!
@IBOutlet weak var saveButton: UIButton!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var usernameLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    profileNavigationBar.delegate = self

    nameTextField.delegate = self
    saveButton.layer.cornerRadius = 3
    if let user = self.user {
        usernameLabel.text = user.username
        if let name = user["name"] as? String {
            nameTextField.text = name
        }
    } else {
        dismissViewControllerAnimated(true, completion: nil)
    }
}

@IBAction func didTapSaveButton(sender: AnyObject) {
    if let user = self.user {
        if nameTextField.text != "" {
            user["name"] = nameTextField.text
            user.saveEventually()
            alertDismiss("Success", message: "New settings saved")
        } else {
        }
    }
}

@IBAction func didTapLogOut(sender: AnyObject) {
    PFUser.logOutInBackgroundWithBlock { error in

        self.performSegueWithIdentifier("showLoginView", sender: self)
    }
}

func checkSetting(user: PFUser, settingName : String) -> Bool {
    if let value = user[settingName] as? Bool {
        return value
    }
    return false
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    self.nameTextField.text = PFUser.currentUser()?["name"] as? String
    self.usernameLabel.text = PFUser.currentUser()?.username
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}
}

1 个答案:

答案 0 :(得分:0)

当您处理PFUsers以避免崩溃时,您需要通过云代码执行此操作,并且不要忘记包含masterKey。我注意到的另一件事是在你的视图控制器中你正在使用user.saveEventually()后跟alertDismiss("Success", message: "New settings saved")这是完全错误的,因为你给用户提供了错误信息,根据解析saveEventually将来某个时候运行并且它没有& #39;有一个完成块,所以实际上你只是在欺骗用户相信保存是成功的。