【发布时间】:2017-09-22 19:24:31
【问题描述】:
我在发出 URL JSON 请求时使用 Dispatch Semaphore 进行等待,此等待可能需要一段时间。为了克服这种情况,我决定创建一个新视图,并在发出请求时将其显示为弹出窗口。为此,我使用了以下代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.showPopUp()
let semaphore = DispatchSemaphore(value: 0)
self.api.requestMedicationsByReagent(method: 1, ean: "", hash: medHash!, gen: generic) { output in
semaphore.signal()
self.objects = output
}
// Thread will wait here until async task closure is complete
let _ = semaphore.wait(timeout: DispatchTime.distantFuture)
}
showPopUp 的作用:
func showPopUp() {
let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "loadingPopUp") as! PopUpViewController
self.addChildViewController(popOverVC)
popOverVC.view.frame = self.view.frame
self.view.addSubview(popOverVC.view)
popOverVC.didMove(toParentViewController: self)
}
问题在于,showPopUp 函数仅在发出请求后才被调用,并且弹出视图只是在屏幕上闪烁。在请求之前如何调用它?
【问题讨论】:
标签: ios swift view popup dispatch