简体   繁体   English

在URLSession.shared.dataTask中执行performSegueWithIdentifier(with:url)

[英]performSegueWithIdentifier while in URLSession.shared.dataTask(with: url)

I am checking on a test Server if a user is registered. 我正在测试服务器上检查用户是否已注册。 If the server gives back, that the username and password are true, i want to perform a Segue with the identifier "Login". 如果服务器返回,则用户名和密码为true,我想使用标识符“ Login”执行Segue。 If I put the performeSegue inside the URLSession dataTask the program crashes with this error: "terminating with uncaught exception of type NSException" I don't know what to do. 如果我将performeSegue放在URLSession dataTask内,程序将因以下错误而崩溃:“终止为NSException类型的未捕获异常”,我不知道该怎么办。 because of the resume() function I can't put the performSegue outside of the dataTask. 由于resume()函数的原因,我不能将performSegue放在dataTask之外。 It would be executed before i get the data from the server. 它会在我从服务器获取数据之前执行。

Here is my Code: 这是我的代码:

@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
    }
}

According to the exception reason you have to perform the segue on the main thread 根据异常原因,您必须在主线程上执行segue

DispatchQueue.main.async {
    self.performSegue(withIdentifier: "Login", sender: self)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 URLSession.shared.dataTask无法下载带有瑞典语网址的图片 - URLSession.shared.dataTask can't download image with swedish url 在URLSession.shared.dataTask之后调用performSegue - Call performSegue after URLSession.shared.dataTask URLSession.shared.dataTask 冻结 UI - URLSession.shared.dataTask freezes the UI iOS:来自 URLSession.shared.dataTask 的数据(带有:url 始终为零(在 xcode 调试器中显示 0 字节错误?) - iOS: Data from URLSession.shared.dataTask(with: url is always nil (display 0 bytes bug in xcode debugger?) 当应用程序处于后台时调用URLSession.shared.dataTask - Invoke URLSession.shared.dataTask when the app is background 在URLSession.shared.dataTask期间从异步关闭存储数据 - Storing data from an asynchronous closure during URLSession.shared.dataTask URLSession.shared.dataTask的完成处理程序内部的闭包 - closure inside completion handler of URLSession.shared.dataTask 使用URLSession.shared.dataTask中的JSON和字典进行Swift 3编译器错误 - Swift 3 Compiler Errors with JSON and Dictionaries within URLSession.shared.dataTask 使用 URLSession.shared.dataTask 发出 api 请求 - Making an api request using URLSession.shared.dataTask 获取URL响应状态代码URLSession.shared.dataTask - Get http response status code of URLSession.shared.dataTask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM