简体   繁体   English

保存新用户Parse Swift时崩溃

[英]Crash when saving new user Parse Swift

So when a user logs in/sign up, they are prompted to enter a name and save it. 因此,当用户登录/注册时,系统会提示他们输入名称并保存。 when they press save the new info is sent to parse and stored. 当他们按保存时,新信息将被发送以进行解析和存储。 My issues is that If i sign up one user give them a name, save, and logout, then sing up a new user, give them a name and press save, the app crashes. 我的问题是,如果我注册了一个用户,给他们一个名字,保存并注销,然后给一个新用户打电话,给他们一个名字,然后按保存,则应用程序崩溃。 I can then rerun the app and save their name again and it works. 然后,我可以重新运行该应用程序并再次保存其名称,它可以正常工作。

I set an exception breakpoint and it breaks on: user.saveInBackground() This is my code for the save button 我设置了一个异常断点,它在以下位置中断:user.saveInBackground()这是我保存按钮的代码

Update: I just learned that whenever I change a users name and save then logout, then sign in with a different user and save a new name for them it crashes. 更新:我刚刚了解到,每当更改用户名并保存然后注销,然后使用其他用户登录并为他们保存新名称时,它就会崩溃。

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

When you deal with PFUsers to avoid crashes you need to do it via cloud code and don't forget to include masterKey. 当您处理PFUser以避免崩溃时,您需要通过云代码进行操作,并且不要忘记包含masterKey。 One more thing i noted is in your view controller you are using user.saveEventually() followed by alertDismiss("Success", message: "New settings saved") which is completely wrong because you are giving the user false information, according to parse saveEventually runs sometime in the future and it doesn't have a completion block so in reality you are just fooling the user in believing that the save was successful. 我注意到的另一件事是,在视图控制器中,您正在使用user.saveEventually()然后使用alertDismiss("Success", message: "New settings saved") ,这是完全错误的,因为根据解析,您向用户提供了虚假信息saveEvent将来会在某个时候运行,并且没有完成块,因此实际上您只是在欺骗用户,以为保存成功。

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

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