performSegueWithIdentifier while in URLSession.shared.dataTask(with: url)
performSegueWithIdentifier while in URLSession.shared.dataTask(with: url)
我正在检查测试服务器是否有用户注册。如果服务器返回,用户名和密码是真的,我想用标识符 "Login" 执行 Segue。如果我将 performeSegue 放在 URLSession dataTask 中,程序会崩溃并出现以下错误:"terminating with uncaught exception of type NSException"
我不知道该怎么办。由于 resume() 函数,我无法将 performSegue 放在 dataTask 之外。它会在我从服务器获取数据之前执行。
这是我的代码:
@IBAction func login(_ sender: AnyObject) {
var session:String?
if userName.text!.isEmpty || logInPassword.text!.isEmpty{
let noUserName = UIAlertController(title: "Kein Benutzername oder Passwort", message: "Bitte geben sie einen Benutzernamen und ein Passwort ein.", preferredStyle: .alert)
noUserName.addAction(OKAction)
self.present(noUserName, animated: true)
} else {
//Implementing URLSession
let urlString = "http://www.***.me/playground/api/v1/user/login/\(userName.text!)/\(logInPassword.text!)"
guard let url = URL(string: urlString) else {
print("Error: couldn't open link")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
}
//Implement JSON decoding and parsing
do {
//Decode retrived data with JSONDecoder and assing type of Article object
let personsData = try JSONDecoder().decode(LoginTest.self, from: responseData)
session = personsData.session
dump(personsData)
} catch let jsonError {
print("Error: \(jsonError)")
}
if session != nil{
//loging in
print("now loged in")
self.performSegue(withIdentifier: "Login", sender: self)
//here it crashes
} else {
let alertWrongPassword = UIAlertController(title: "Benutzername und Passwort stimmen nicht überein", message: "Bitte versuchen Sie es erneut.", preferredStyle: .alert)
alertWrongPassword.addAction(self.OKAction)
self.present(alertWrongPassword, animated: true)
}
}.resume()
//End implementing URLSession
}
}
根据异常原因必须在主线程上执行segue
DispatchQueue.main.async {
self.performSegue(withIdentifier: "Login", sender: self)
}
我正在检查测试服务器是否有用户注册。如果服务器返回,用户名和密码是真的,我想用标识符 "Login" 执行 Segue。如果我将 performeSegue 放在 URLSession dataTask 中,程序会崩溃并出现以下错误:"terminating with uncaught exception of type NSException" 我不知道该怎么办。由于 resume() 函数,我无法将 performSegue 放在 dataTask 之外。它会在我从服务器获取数据之前执行。
这是我的代码:
@IBAction func login(_ sender: AnyObject) {
var session:String?
if userName.text!.isEmpty || logInPassword.text!.isEmpty{
let noUserName = UIAlertController(title: "Kein Benutzername oder Passwort", message: "Bitte geben sie einen Benutzernamen und ein Passwort ein.", preferredStyle: .alert)
noUserName.addAction(OKAction)
self.present(noUserName, animated: true)
} else {
//Implementing URLSession
let urlString = "http://www.***.me/playground/api/v1/user/login/\(userName.text!)/\(logInPassword.text!)"
guard let url = URL(string: urlString) else {
print("Error: couldn't open link")
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
}
//Implement JSON decoding and parsing
do {
//Decode retrived data with JSONDecoder and assing type of Article object
let personsData = try JSONDecoder().decode(LoginTest.self, from: responseData)
session = personsData.session
dump(personsData)
} catch let jsonError {
print("Error: \(jsonError)")
}
if session != nil{
//loging in
print("now loged in")
self.performSegue(withIdentifier: "Login", sender: self)
//here it crashes
} else {
let alertWrongPassword = UIAlertController(title: "Benutzername und Passwort stimmen nicht überein", message: "Bitte versuchen Sie es erneut.", preferredStyle: .alert)
alertWrongPassword.addAction(self.OKAction)
self.present(alertWrongPassword, animated: true)
}
}.resume()
//End implementing URLSession
}
}
根据异常原因必须在主线程上执行segue
DispatchQueue.main.async {
self.performSegue(withIdentifier: "Login", sender: self)
}