IOS Swift 我怎样才能 return 标签响应更快
IOS Swift how can I return label response faster
我正在使用休息 API 登录 我只是发送一个包含用户电子邮件和密码的请求。如果它们在数据库中匹配,那么我 return 一些 Json 和 swift 解析它。那部分工作正常我现在想做的是弄清楚如果电子邮件和密码不匹配我如何提醒用户。到目前为止我得到了这个
@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 。这意味着 email/password 组合不起作用,如果 email/password 组合起作用,那么我将其设置为 1 。问题是如果用户提供了错误的凭据,大约需要 5 或 7 秒才能显示 Incorrect email and/or password 消息。当我检查呼叫响应时间时,它会立即返回,但标签显示需要 7 秒。如果我将相同的逻辑放在 .resume 之外,那么它会立即出现但是消息总是不正确的电子邮件和密码无论检查是 0 还是 1 任何建议都会更好
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
这些行在完成处理程序中,但尚未分派到主队列。后台队列中的完成块 运行s。您不能修改除主队列以外的任何队列上的 UI 个组件。如果这样做,有时它会起作用,但您看到的症状(长时间延迟)是您可以 运行 遇到的众多问题之一。将它们移动到 DispatchQueue.main.async
中,就像在 do
块中所做的那样。
我正在使用休息 API 登录 我只是发送一个包含用户电子邮件和密码的请求。如果它们在数据库中匹配,那么我 return 一些 Json 和 swift 解析它。那部分工作正常我现在想做的是弄清楚如果电子邮件和密码不匹配我如何提醒用户。到目前为止我得到了这个
@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 。这意味着 email/password 组合不起作用,如果 email/password 组合起作用,那么我将其设置为 1 。问题是如果用户提供了错误的凭据,大约需要 5 或 7 秒才能显示 Incorrect email and/or password 消息。当我检查呼叫响应时间时,它会立即返回,但标签显示需要 7 秒。如果我将相同的逻辑放在 .resume 之外,那么它会立即出现但是消息总是不正确的电子邮件和密码无论检查是 0 还是 1 任何建议都会更好
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
这些行在完成处理程序中,但尚未分派到主队列。后台队列中的完成块 运行s。您不能修改除主队列以外的任何队列上的 UI 个组件。如果这样做,有时它会起作用,但您看到的症状(长时间延迟)是您可以 运行 遇到的众多问题之一。将它们移动到 DispatchQueue.main.async
中,就像在 do
块中所做的那样。