gpt4 book ai didi

ios - 如何等待嵌套的 Alamofire 请求完成

转载 作者:搜寻专家 更新时间:2023-11-01 07:25:16 24 4
gpt4 key购买 nike

我有两个 Alamofire 请求。两者都可以自己正常工作。

func getPatientID() {  //puts patientID into patientID variable

UsingOauth2(drChronoOauth2Settings, performWithToken: { token in
Router.OAuthToken = token
apiCallText = "last_name=" + self.lastName.text! + "&" + "first_name=" + self.firstName.text!
Alamofire.request(Router.GetPatientsWithFilter())
.responseJSON(completionHandler: { (result) -> Void in
if let data = result.data {
let response = NSString(data: data, encoding: NSUTF8StringEncoding)
self.result.text = "\(response)"

json = JSON(data: data)
}

self.result.text = ""
if json!["count"].intValue > 1 {
self.result.text = "more than one patient"
patientID = "-1"
} else {
for item in json!["results"].arrayValue {
patientID = item["id"].stringValue
self.result.text = ("Patient ID is: " + patientID!)
}
}

})

}, errorHandler: {
print("Oauth2 failed")

})

view.endEditing(true)

}

还有……

func postPDF() {

getPatientID() //NEED TO WAIT FOR COMPLETION


UsingOauth2(drChronoOauth2Settings, performWithToken: { token in
DrChronoRequestConvertible.OAuthToken = token

Alamofire.upload(
.POST,
"https://drchrono.com/api/documents",
headers: ["Authorization" : "Bearer " + token],
multipartFormData: { multipartFormData in

//some multiform data including patientID from getPatientID()

},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
self.result.text = "Successful Upload"
}
case .Failure(let encodingError):
print(encodingError)
}
}
) }, errorHandler: {
print("Oauth2 failed")

})
}

以上代码将无法运行,因为“getPatientID”函数未完成。我知道我知道我必须以某种方式使用调度或完成处理程序。但我觉得这个话题很困惑。我在这里查看了类似的解决方案,但找不到适合我的解决方案。

最佳答案

您可以将 postPDF 调用嵌套在 getPatientID 的完成处理程序中,如下所示:

func getPatientID() {  //puts patientID into patientID variable

UsingOauth2(drChronoOauth2Settings, performWithToken: { token in
Router.OAuthToken = token
apiCallText = "last_name=" + self.lastName.text! + "&" + "first_name=" + self.firstName.text!
Alamofire.request(Router.GetPatientsWithFilter())
.responseJSON(completionHandler: { (result) -> Void in
if let data = result.data {
let response = NSString(data: data, encoding: NSUTF8StringEncoding)
self.result.text = "\(response)"

json = JSON(data: data)
}

self.result.text = ""
if json!["count"].intValue > 1 {
self.result.text = "more than one patient"
patientID = "-1"
} else {
for item in json!["results"].arrayValue {
patientID = item["id"].stringValue
self.result.text = ("Patient ID is: " + patientID!)
}
}

// Now that getPatientID has completed, call the next function
postPDF()

})

}, errorHandler: {
print("Oauth2 failed")

})

view.endEditing(true)

}

关于ios - 如何等待嵌套的 Alamofire 请求完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778310/

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