gpt4 book ai didi

ios - 在执行 segue 之前如何检查服务器响应值

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

这是我在 UploadModel.swift 中的代码

func uploadSpecialistWithId(login: String, password: String) {
let requestUrl = URL(string: "http://localhost:3000/api/register/specialist")!
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
let postParams = "login=\(login)&password=\(password)"
print(postParams)
request.httpBody = postParams.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) {(data, response, error)
in
if(error != nil) {
print("error -> \(error!.localizedDescription)")
} else {
print("data uploaded")
self.parseJSONSpecialistResponse(data: data!)
}

}
task.resume()
}

func parseJSONSpecialistResponse(data: Data) {

var jsonResult = NSDictionary()
let specialist = Specialist()
do {
jsonResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary
} catch let error as NSError {
print("error -> \(error.localizedDescription)")
}

let items = NSMutableArray()

if let login = jsonResult["login"] as? String,
let specialistId = jsonResult["_id"] as? String {
specialist.login = login
specialist.specialistId = specialistId
print("Added \(specialist.itemDescribtion())")
items.add(specialist)
} else {
let errorCode = jsonResult["code"] as! Int
print("Erorr with code -> \(errorCode)")
items.add(errorCode)
}

DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsUploaded(items: items)
})
}

但是当我尝试使用现有登录名注册专家时,segue 在我处理服务器响应之前就已经执行了。我应该改变什么来解决这个问题?这是我在 ViewController.swift 中的代码

func itemsUploaded(items: NSArray) {
if let errorCode = items[0] as? Int {
if errorCode == 11000 {
warningLabel.text = "This login is already used"
error = true
}
}
if let specialist = items[0] as? Specialist {
self.specialistId = specialist.specialistId!
print("Value after itemsUploaded --> \(self.specialistId)")
}
}

@IBAction func doneButtonTouched(_ sender: Any) {
uploadModel.uploadSpecialistWithId(login: loginTextField.text!, password: passwordTextField.text!)
if(error == false) {
print("Segue perform value -> \(specialistId)")
performSegue(withIdentifier: "regToRegCompleteSegue", sender: self)
}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! RegistrationCompleteViewController
print("Segue prepare value -> \(specialistId)")
destination.specialistId = self.specialistId
}

据我了解,由于 dataTask 异步,我遇到了此问题。但是为什么当 self.delegate 已经在主队列中时它不起作用?

编辑:添加completionHandler并没有解决我的问题,但卡姆兰的提议正在发挥作用。

最佳答案

完成响应后,您应该通过移动 itemsUploaded 中的以下行来执行 segue,如下所示,

@IBAction func doneButtonTouched(_ sender: Any) {
uploadModel.uploadSpecialistWithId(login: logiTextField.text!, password: passwordTextField.text!)
}

func itemsUploaded(items: NSArray) {
if let errorCode = items[0] as? Int {
if errorCode == 11000 {
warningLabel.text = "This login is already used"
error = true
}
}
if let specialist = items[0] as? Specialist {
self.specialistId = specialist.specialistId!
print("Value after itemsUploaded --> \(self.specialistId)")
}

if(error == false) {
print("Segue perform value -> \(specialistId)")
performSegue(withIdentifier: "regToRegCompleteSegue", sender: self)
}
}

关于ios - 在执行 segue 之前如何检查服务器响应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50316501/

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