Search code examples
iosswiftnetwork-programmingibaction

Cannot Perform Any Code in IBAction after getting network request


I am trying to transition into a new segue after I confirm that I get a the session ID from the Udacity API. However, I cannot get any code to run after this. The session ID and all the information will print, but nothing will complete after. Is there something I am doing wrong in the function? I tried performing the UIUpdates then just tried printing "Hello" but that has not worked either. Below is some code and also my GitHub profile. https://github.com/SteveBurgos95/UdacityMapProject

    @IBAction func loginButton(_ sender: Any) {
UdacityClient.sharedInstance().loginWithUsername(emailTextField.text!, password: passwordTextField.text!){ (success, error) in
    performUIUpdatesOnMain {
        if success {
            print("Hello")
        } else {
            print("Hey")
        }
    }
}

private func completeLogin() {
emailTextField.text = ""
let controller = storyboard!.instantiateViewController(withIdentifier: "TableMapViewController") as! UINavigationController
present(controller, animated: true, completion: nil)

Solution

  • It seems to me like you're never calling your completionhandler in UdacityClient.swift when the dataTask finishes (both in case of error and success)?

    This should be something like this:

    let task = session.dataTask(with: request as URLRequest) { data, response, error in
    
        // Make sure there is no error, or else
        guard error == nil else { // Handle error…
    
          completionHandler(false, error?.localizedDescription)
          /*
             func sendError(_ error: String) {
             print(error)
             let userInfo = [NSLocalizedDescriptionKey : error]
             completionHandlerForGET(nil, NSError(domain: "taskForGETMethod", code: 1, userInfo: userInfo))
            */
            return
        }
    
        completionHandler(true, nil)
    }