ios - How to wait for a nested Alamofire request to complete -


i have 2 alamofire requests. both work fine on own.

func getpatientid() {  //puts patientid 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 1 patient"                         patientid = "-1"                     } else {                         item in json!["results"].arrayvalue {                             patientid = item["id"].stringvalue                             self.result.text = ("patient id is: " + patientid!)                     }                 }              })          }, errorhandler: {             print("oauth2 failed")      })  view.endediting(true)  } 

and...

func postpdf() {      getpatientid()  //need wait 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 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")      }) } 

the above code won't work because "getpatientid" function isn't complete. know know have use dispatch or completion handler somehow. find topic confusing. have looked through similar solutions here, can't find 1 works me.

you can nest postpdf call inside completion handler of getpatientid this:

func getpatientid() {  //puts patientid 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 1 patient"                     patientid = "-1"                 } else {                     item in json!["results"].arrayvalue {                         patientid = item["id"].stringvalue                         self.result.text = ("patient id is: " + patientid!)                 }             }              // getpatientid has completed, call next function             postpdf()          })      }, errorhandler: {         print("oauth2 failed")  })  view.endediting(true)  } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -

python - pdfminer - import error -