在登录页面中,我使用闭包从帮助程序类接收 API 响应。收到回复后,我正在导航到另一个页面。该控件执行 pushViewController 行,但导航需要很长时间。是因为关闭吗?我该如何解决它?
class LandingView: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getStaticCountryAndStateList()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Navigation
func getStaticCountryAndStateList()
{
WebAPIHelper.getMethod(methodName: "/Common/GetStaticCountryAndStateList", success: {(response)->Void in
let signInObj = SignIn(nibName: (UIDevice.current.userInterfaceIdiom == .pad ? "SignIn" : "SignIn_iPhone"), bundle: nil)
DispatchQueue.main.async
{
self.navigationController?.pushViewController(signInObj, animated: true)
}
}, Failure: {
(error)->Void in
print("Error (error)")
})
}
}
To push view controller fast in closure, you must access main queue.
Below is code for accessing main queue in swift.
func getStaticCountryAndStateList()
{
WebAPIHelper.getMethod(methodName: "/Common/GetStaticCountryAndStateList", success: {(response)->Void in
DispatchQueue.main.async {
let signInObj = Payer(nibName: (UIDevice.current.userInterfaceIdiom == .pad ? "Payer" : "Payer_iPhone"), bundle: nil)
self.navigationController?.pushViewController(signInObj, animated: true)
}
}, Failure: {
(error)->Void in
print("Error (error)")
})
}
}
在与您的 Web 请求不在同一线程上的主线程上推送视图。
始终在主线程中执行与 UI 相关内容的内容。
dispatch_async(dispatch_get_main_queue(), ^{
//update UI
});
斯威夫特 3
DispatchQueue.main.async {
// update ui
}