我有两个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”函数未完成。我知道我知道我必须以某种方式使用调度或完成处理程序。但我觉得这个话题很混乱。我在这里看过类似的解决方案,但找不到适合我的解决方案。
答案 0 :(得分:1)
您可以将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)
}
的完成处理程序中,如下所示:
Insert Into @WarrantBail
EXEC (@TSQL)
END
BEGIN
-- Create a Variable
DECLARE @NoBailCount int
答案 1 :(得分:1)
您可以在getPatientId函数中添加完成处理程序。
System.Web.Helpers.Crypo.HashPassword()
和
func getPatientID(completion: (id: String?) -> Void) { //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!)
}
}
completion(patientID)
})
}, errorHandler: {
print("Oauth2 failed")
completion(nil)
})
view.endEditing(true)
}