2017-01-10 5 views
0

ランディングページでは、クロージャーを使用してヘルパークラスからapi応答を受け取りました。私が応答を受け取ると、別のページにナビゲートしています。コントロールはpushViewController行を実行しますが、ナビゲートには時間がかかります。閉鎖のためですか?どうすれば修正できますか?ViewControllerクロージャー内にプッシュされたときに長時間遅れてプッシュ

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)") 
    }) 
} 
} 
+1

あなたのAPIからの応答はおそらく時間がかかりすぎています。だからこそ、ViewControllerは長い遅延の後にプッシュしています。 –

答えて

0

常にUI関連のものをメインスレッドで実行します。

dispatch_async(dispatch_get_main_queue(), ^{ 
     //update UI 
}); 

スウィフト3

DispatchQueue.main.async { 
     // update ui 
    } 
+0

DispatchQueue.main.async {}はシミュレータでうまくいきますが、iPadやiPhoneなどの実際のデバイスには違いはありません。 – Mano

+0

@Mano、どういう意味ですか?**実際のデバイスでは何の違いもありません** –

1
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)") 
    }) 
} 
} 
+0

DispatchQueue.main.async {}はシミュレータでうまく動作しますが、実際のデバイスには違いはありません。 iPadまたはiPhone。 – Mano

0

プッシュではないあなたのWeb要求と同じスレッド上でメインスレッド上のビュー。

+0

あなたが意味するコードサンプルを私に提供することはできますか – Mano

関連する問題