Home > front end >  I'm trying to make a Login alert in SWIFT but somehow the code doesn't work
I'm trying to make a Login alert in SWIFT but somehow the code doesn't work

Time:06-14

@IBAction func login(_ sender: Any) {

     PFUser.logInWithUsername(inBackground: self.txtEmailSignin.text!, password: self.txtPasswordSignin.text!) {

        (user: PFUser?, error: Error?) -> Void in
        if user != nil {
            self.displayAlert(withTitle: "Login Successfully", message: "")
        } else {
            self.displayAlert(withTitle: "Error", message: error!.localizedDescription)
        }
    }
}

CodePudding user response:

First of all, You need to represent the code properly. Your provided code is difficult to understand.

CodePudding user response:

When ever you want to update any UI element in iOS app, the UI should be always updated in the main queue.

Note: Here in your sample code you are performing login request in the background thread it seems. So you are required to update the alert UI in the main thread.

@IBAction func login(_ sender: Any) {

PFUser.logInWithUsername(inBackground: self.txtEmailSignin.text!, password: self.txtPasswordSignin.text!) {
    (user: PFUser?, error: Error?) -> Void in
    if user != nil {
        DispatchQueue.main.async {
            self.displayAlert(withTitle: "Login Successfully", message:"")
        }
    } else {
        DispatchQueue.main.async {
            self.displayAlert(withTitle: "Error", message: error!.localizedDescription)
        }
    }
}}
  • Related