0

I am using a rest API for logging in I simply send a request with the user's email and password . If they match in the database then I return some Json and swift parses it . That part is working correctly what I am trying to do now is figure out how I can alert a user if that the email and password don't match. So far I got this

  @IBAction func Login_Action(_ sender: Any) {
        var check = 0
        let url:URL = URL(string:ConnectionString+"login")!
        let Email = email.text!
        let Password = password.text!
        let session = URLSession.shared
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        let parameter = "email=\(Email)&password=\(Password)"
        request.httpBody = parameter.data(using: String.Encoding.utf8)

        session.dataTask(with:request, completionHandler: {(data, response, error) in
            if error != nil {
                 // print(error)
            } else {
                do {
         // if the call is successful then it goes through here otherwise it skips it
                   check = 1


                    let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]




                        }
                        DispatchQueue.main.async {

            // send them to member page            
                        let next = self.storyboard?.instantiateViewController(withIdentifier: "NextView")
                            self.present(next, animated: true, completion: nil)

                        }


                    }

                } catch let error as NSError {
                    print(error)
                     if check == 0 {
                self.error.isHidden = false
                self.error.text = "Incorrect email and/or password"
            }
                }


            }
       // If the call is unsuccessful then show message

            return
        }).resume()
        /*
        print(check)
        if check == 0 {
            self.error.isHidden = false
            self.error.text = "Incorrect email and/or password"
            print("incorrect")
        }*/


    }

I am using the check variable as the flag it's default is 0 . Which means that the email/password combination does not work and if the email/password combination works then I set it to 1 . The issue is that it takes about 5 or 7 seconds to display the Incorrect email and/or password message if the user supplied the wrong credentials . When I check the call response time it comes back immediately yet it takes a good 7 seconds for the label to show . If I put that same logic outside the .resume then it appears instantly however the message is always incorrect email and password whether or not the check is 0 or 1 any suggestions would be better

Rome Torres
  • 1,061
  • 2
  • 13
  • 27
  • 1
    Hey Rome, pls improve your code formatting — it's very hard to read it as is, thanks! Also: keep in mind that `guard` is your friend :-) – Paulo Mattos Apr 11 '17 at 23:12
  • 1
    You need to show the information about incorrect login details *inside the closure*, just as you do with the presentation of the next scene. Remember to dispatch the UI update on the main queue – Paulw11 Apr 11 '17 at 23:12
  • Thanks will do Paulo and oh ok Paulw will do that now – Rome Torres Apr 11 '17 at 23:17

1 Answers1

1
            self.error.isHidden = false
            self.error.text = "Incorrect email and/or password"

These lines are in the completion handler, but have not been dispatched to the main queue. The completion block runs on a background queue. You cannot modify UI components on any queue but the main queue. If you do, sometimes it will work, but the symptom you're seeing (long delays) is one of many problems you can run into. Move these into a DispatchQueue.main.async just as you did inside the do block.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610