簡體   English   中英

保存新用戶Parse Swift時崩潰

[英]Crash when saving new user Parse Swift

因此,當用戶登錄/注冊時,系統會提示他們輸入名稱並保存。 當他們按保存時,新信息將被發送以進行解析和存儲。 我的問題是,如果我注冊了一個用戶,給他們一個名字,保存並注銷,然后給一個新用戶打電話,給他們一個名字,然后按保存,則應用程序崩潰。 然后,我可以重新運行該應用程序並再次保存其名稱,它可以正常工作。

我設置了一個異常斷點,它在以下位置中斷: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
}
}

當您處理PFUser以避免崩潰時,您需要通過雲代碼進行操作,並且不要忘記包含masterKey。 我注意到的另一件事是,在視圖控制器中,您正在使用user.saveEventually()然后使用alertDismiss("Success", message: "New settings saved") ,這是完全錯誤的,因為根據解析,您向用戶提供了虛假信息saveEvent將來會在某個時候運行,並且沒有完成塊,因此實際上您只是在欺騙用戶,以為保存成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM