【发布时间】:2020-06-11 08:27:25
【问题描述】:
我是 swift 新手,我正在尽力而为。
当它执行时我有主要功能appointmentCall(),作为响应,我可能会得到多个约会。然后我将appointmentId 传递给appointmentDetail 函数以获取更多详细信息。
我想要的只是如何将 For 循环设置为同步过程。意味着它不会执行下一个约会,直到第一次完成。目前它执行所有约会。
我需要一个一个约会执行所有功能,一旦完成执行下一个约会。
预约电话 -> AppoinmentDetail -> processDetail -> 已完成。
代码:
func appointmentCall(_ selectedDate:Date) {
DataProvider.main.serviceGetAppointment(date: selectedDate, callback: {success, result in
do{
if(success){
print(result as! Data)
let decoder = JSONDecoder()
let response = try decoder.decode(ResponseData.self, from: result! as! Data)
if let appointments = response.appointments {
self.appData = appointments.map { AppointmentDownloadModel(appointmentModel: $0)}
}
for eachApp in self.appData {
self.appointmentDetail(AppId: appId)
}
self.tableView.reloadData()
return true
}else{
return false
}
}catch let error {
DataProvider.main.token = nil
print(error as Any)
return false
}
})
}
func appointmentDetail(AppId: Int){
DataProvider.main.serviceGetAppointmentDetail(Id: AppId , callback: {success, result in
do{
if(success){
let decoder = JSONDecoder()
let resp = try decoder.decode(AppointmentDetail.self, from: result! as! Data)
self.AppDetailData = resp
self.processDetail(appId: AppId)
return true
}else{
return false
}
}catch let error {
print(error as Any)
return false
}
})
}
func processDetail(appId: Int) {
guard let detail = AppDetailData, AppDetailData?.appointmentId == appId else {
return
}
for firmParam in (detail.sectionList ?? []) {
for firmItem in firmParam.items! {
if firmItem.actionParamData != nil {
let str = firmItem.actionParamData
let param = str?.components(separatedBy: ":")
let final = param![1].replacingOccurrences(of: "}", with: "")
let fmId = final.components(separatedBy: ",")
let frmId = fmId[0]
self.firmDetails(actionParamData: Int(frmId) ?? 0)
}
//pdf download
if firmItem.actionType == 2 {
if firmItem.actionUrl != nil {
self.contentLength(link: firmItem.actionUrl!)
let fileURL = URL(fileURLWithPath: firmItem.actionUrl ?? "")
let fileTitle = firmItem.textField ?? ""
self.downloadPDFTask(pdfURL: firmItem.actionUrl ?? "")
}
}
}
}
}
【问题讨论】:
-
您可以使用递归函数调用另一个具有完成处理程序的函数,在该完成处理程序中再次调用该函数,并从成员变量列表中获取您的值,每次函数完成时它只是再次调用自己,您会知道它们只有在每个完成后才按顺序处理
-
@AngryDuck 你能举个例子吗?谢谢?因为
appointmentDetail已经在调用appointmentCall函数。 -
查看我对高级通用示例的回答
标签: ios swift asynchronous synchronization