【问题标题】:IOS Swift how can I return label response fasterIOS Swift如何更快地返回标签响应
【发布时间】:2017-04-11 23:07:31
【问题描述】:

我正在使用 rest API 进行登录,我只需发送一个带有用户电子邮件和密码的请求。如果它们在数据库中匹配,那么我会返回一些 Json 并快速解析它。这部分工作正常我现在要做的是弄清楚如果电子邮件和密码不匹配,我如何提醒用户。到目前为止,我得到了这个

  @IBAction func Login_Action(_ sender: Any) {
        var check = 0
        let url:URL = URL(string:ConnectionString+"login")!
        let Email = email.text!
        let Password = password.text!
        let session = URLSession.shared
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
        let parameter = "email=\(Email)&password=\(Password)"
        request.httpBody = parameter.data(using: String.Encoding.utf8)

        session.dataTask(with:request, completionHandler: {(data, response, error) in
            if error != nil {
                 // print(error)
            } else {
                do {
         // if the call is successful then it goes through here otherwise it skips it
                   check = 1


                    let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]




                        }
                        DispatchQueue.main.async {

            // send them to member page            
                        let next = self.storyboard?.instantiateViewController(withIdentifier: "NextView")
                            self.present(next, animated: true, completion: nil)

                        }


                    }

                } catch let error as NSError {
                    print(error)
                     if check == 0 {
                self.error.isHidden = false
                self.error.text = "Incorrect email and/or password"
            }
                }


            }
       // If the call is unsuccessful then show message

            return
        }).resume()
        /*
        print(check)
        if check == 0 {
            self.error.isHidden = false
            self.error.text = "Incorrect email and/or password"
            print("incorrect")
        }*/


    }

我使用 check 变量作为默认标志 ​​0 。这意味着电子邮件/密码组合不起作用,如果电子邮件/密码组合起作用,那么我将其设置为 1 。问题在于,如果用户提供了错误的凭据,则显示 Incorrect email and/or password 消息大约需要 5 或 7 秒。当我检查呼叫响应时间时,它会立即返回,但标签显示需要 7 秒。如果我将相同的逻辑放在 .resume 之外,那么它会立即出现,但是无论检查是 0 还是 1,消息总是不正确的电子邮件和密码,任何建议都会更好

【问题讨论】:

  • 嘿,罗马,请改进您的代码格式 - 很难按原样阅读,谢谢!另外:请记住,guard 是您的朋友 :-)
  • 您需要在闭包内显示有关错误登录详细信息的信息,就像下一个场景的演示一样。记得在主队列上调度 UI 更新
  • 谢谢 Paulo,哦,好的,Paulw 现在会这样做

标签: ios swift swift3 nsurlsessiondatatask


【解决方案1】:
            self.error.isHidden = false
            self.error.text = "Incorrect email and/or password"

这些行在完成处理程序中,但尚未分派到主队列。完成块在后台队列上运行。除了主队列,您不能修改任何队列上的 UI 组件。如果您这样做,有时它会起作用,但您看到的症状(长时间延迟)是您可能遇到的许多问题之一。就像您在 do 块中所做的那样,将它们移动到 DispatchQueue.main.async 中。

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多