gpt4 book ai didi

IOS Swift 如何更快地返回标签响应

转载 作者:行者123 更新时间:2023-11-28 15:44:16 25 4
gpt4 key购买 nike

我正在使用 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 。问题是,如果用户提供了错误的凭据,大约需要 5 或 7 秒才能显示不正确的电子邮件和/或密码消息。当我检查调用响应时间时,它会立即返回,但标签显示需要 7 秒。如果我将相同的逻辑放在 .resume 之外,那么它会立即出现,但是消息总是不正确的电子邮件和密码,无论支票是 0 还是 1,任何建议都会更好

最佳答案

            self.error.isHidden = false
self.error.text = "Incorrect email and/or password"

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

关于IOS Swift 如何更快地返回标签响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com