简体   繁体   中英

how to wait for the URLSession to finish before returning the result from a function in Swift 3

Hi i have a beginner question, where i cant find a good solution for in Swift 3. I hope someone is able to hlep.

i have the following code that will check a rest api if the user credentials are valid or not. I want it to wait for the resquest is finished and then return true or false. now it is being send async.

Also any improvement in the way i check the JSON value's would be welcome too.

func CheckUsernamePassword(username :String ,code:String )-> bool {

    var validCredentials = false


    let urlString = "\(self.baseurl)/accounts/validateusernamepassword.json?username=\(username)&password=\(code)&api_key=\(self.api_key)"

    let url = URL(string: urlString)
    URLSession.shared.dataTask(with:url!) { (data, response, error) in
        if error != nil {
            print("Error URLSession : \(error!)")
            validCredentials = false
        } else {
            do {
                let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]

                if parsedData["validated"] != nil {
                    if "\(parsedData["validated"]!)" == "1" {
                        print("Login credentials are correct")
                        validCredentials = true             
                    }else {
                        print("Login credentials are not correct")
                        print("\(parsedData["validated"]!)")
                        print("\(parsedData["message"]!)")
                        validCredentials = false
                    }
                }else{
                    print("Json Parse error: \(parsedData)")
                    validCredentials = false
                }
            } catch let error as NSError {
                print("Error Parsing Json \(error)" )
                validCredentials = false
            }
        }

        }.resume()
    return validCredentials          
}

You cannot return something from an asynchronous task as a return value.

Do not wait , use a completion handler:

  • Replace the signature of the method (the name is supposed to start with a lowercase letter) with

     func checkUsernamePassword(username: String, code: String, completion: @escaping (Bool)->() ) { 
  • Delete the lines var validCredentials = false and return validCredentials

  • Replace all occurrences of validCredentials = false with completion(false) and validCredentials = true with completion(true) .

  • Call the method

     checkUsernamePassword(username: "Foo", code: "Baz") { isValid in print(isValid) // do something with the returned Bool DispatchQueue.main.async { // update UI } } 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM