2016-05-20 5 views
0

私はSwiftで初めて作業しています。私は以下のようにWeb APIを起動しています。データは正常に取得されていますが、何も実行できません。NSURLConnection.sendAsynchronousRequest black。私はsegueを実行するメソッドを書いた後、結果を印刷していますが、別のViewControllerを押さずに、同じView Controllerにとどまります。前もって感謝します。SwiftでAsynchronusRequestの後に何も実行できません

NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in 
      //SwiftLoader.hide() 
      do { 
       if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { 
        SwiftLoader.hide() 
        print("ASynchronous\(jsonResult)") 
        self.performSegueWithIdentifier("Home", sender: self) 
       } 
       self.hideView() 
      } 
      catch let error as NSError 
      { 
       print(error.localizedDescription) 
      } 
     }) 

答えて

2

セグを実行したい場合は、バックグラウンドスレッドではなくメインスレッドで実行する必要があります。 NSURLConnection.sendAsynchronousRequestはバックグラウンドスレッド上で実行されるので、UI関連の変更が必要な場合は、メインスレッドを使用する必要があります。ですので、このように使用してください: -

NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in 
     //SwiftLoader.hide() 
     do { 
      if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { 
       SwiftLoader.hide() 
       print("ASynchronous\(jsonResult)") 
       dispatch_sync(dispatch_get_main_queue(), { 
        self.performSegueWithIdentifier("Home", sender: self)           
       }) 

      } 
      self.hideView() 
     } 
     catch let error as NSError 
     { 
      print(error.localizedDescription) 
     } 
    }) 
2

メインスレッドでUI操作を実行する必要があります。現在、ワーカースレッド上で実行しています。 GCDの助けを借りて、メインスレッドを取得し、Segue操作をディスパッチします。

dispatch_async(dispatch_get_main_queue(),{ 

    print("ASynchronous\(jsonResult)") 
    self.performSegueWithIdentifier("Home", sender: self) 
}) 
+0

私は理解できませんでしたあなたはもう少し教えてください –

関連する問題