how to wait for the URLSession to finish before re

2019-04-02 11:41发布

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          
}

1条回答
闹够了就滚
2楼-- · 2019-04-02 12:03

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
        }
    }
    
查看更多
登录 后发表回答