gpt4 book ai didi

ios - 等待 session.dataTask 上的任务返回

转载 作者:行者123 更新时间:2023-11-30 11:53:27 26 4
gpt4 key购买 nike

我有一个项目,我通过 API 发送和获取数据。发送和获取工作正常。

我的目标是等待服务器的响应(即:是|否),并根据它,是否继续继续,如果否则显示警报。

对于这部分我有:

  1. 带有客户端列表和添加新客户端的按钮的表格 View 。
  2. 我在其中添加/编辑客户的详细 View
  3. 在详细信息 View 上有一个保存按钮,可连接到表格 View

函数saveClient()获取数据并向服务器发出请求

func saveClient() -> Bool {

let name = txtName.text ?? ""
let address = txtAddress.text ?? ""
let city = txtCity.text ?? ""
let province = txtProvince.text ?? ""
let postal_code = txtPostalCode.text ?? ""

meal = Client(name:name, client_id: "", postal_code: postal_code, province: province, city: city, address: address)

var jsonData = Data()
let jsonEncoder = JSONEncoder()
do {
jsonData = try jsonEncoder.encode(meal)
}
catch {
}
print("a1")
var success: Bool
success = false

makeRequestPost(endpoint: "http://blog.local:4711/api/clients/add",
requestType: "POST",
requestBody: jsonData,
completionHandler: { (response : ApiContainer<Client>?, error : Error?) in
if let error = error {
print("error calling POST on /todos")
print(error)
return
}
let b = (response?.meta)!
print(b.sucess)
if(b.sucess == "yes") {
success = true
}
else
{
DispatchQueue.main.async(execute: {
let myAlert = UIAlertController(title: "Error", message: "Error creating Client", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
myAlert.addAction(okAction)
self.present(myAlert, animated: true, completion: nil)
})
return
}
} )
return success
}

在同一个 Controller 上:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

guard let button = sender as? UIBarButtonItem, button === btnSave else {
return false
}

if !saveClient() {
print("no sir")
return false
}

print("yes sir")
return true
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard let button = sender as? UIBarButtonItem, button === btnSave else {
return
}
}

请求函数:

func makeRequestPost<T>(endpoint: String,
requestType: String = "GET",
requestBody: Data, completionHandler: @escaping (ApiContainer<T>?, Error?) -> ()) {

guard let url = URL(string: endpoint) else {
print("Error: cannot create URL")
let error = BackendError.urlError(reason: "Could not create URL")
completionHandler(nil, error)
return
}

var urlRequest = URLRequest(url: url)
let session = URLSession.shared

urlRequest.httpMethod = "POST"
urlRequest.httpBody = requestBody

urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")



let task = session.dataTask(with: urlRequest, completionHandler: {
(data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
completionHandler(nil, error)
return
}
guard error == nil else {
completionHandler(nil, error!)
return
}

do {
let response = try JSONDecoder().decode(ApiContainer<T>.self, from: responseData)
completionHandler(response, nil)
}
catch {
print("error trying to convert data to JSON")
print(error)
completionHandler(nil, error)
}

})
task.resume()
}

在控制台上我得到:

a1

no sir

yes

正确的是:

a1

yes

yes sir

最后的注释:

  1. 我尝试了一些带有信号量的示例...但没有成功。

  2. 我在其他地方使用名为 Meal 的 var,但尚未将其更改为客户端。但是,它不会干扰那部分代码

最佳答案

你说:

My goal is to wait a response (ie: yes|no) from the server and depending on it, proceed with the segue or not

虽然我理解这种方法的自然吸引力,但现实是您永远不应该“等待”(尤其是在 shouldPerformSegue 中)。您的 UI 将卡住(如果用户的互联网连接较差,则尤其值得注意),您的应用程序很容易被寻找卡住应用程序的看门狗进程杀死,等等。

因此,不要执行segue并让shouldPerformSegue尝试等待网络响应以查看是否可以继续,而是以相反的方式进行:您可以编写执行查询的代码如果一切正常,则只能以编程方式启动 segue。

关于ios - 等待 session.dataTask 上的任务返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100651/

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