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
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.