2016-11-06 31 views
0

對不起,對於初學者的問題。 我有一個操作取決於從網絡返回的數據的結果,並且操作可能需要另一個網絡請求。由於第一個網絡請求已經在datatask中調用過了,我想爲第二個網絡請求使用同一個線程,但不知道如何去做。任何提示? TKS迅速,在同一任務的線程中另一次調用網絡

let task = URLSession.shared.dataTask(with: request as URLRequest) 
     { data, response, error in 

      if func_1 (data) { 
       return 
      }else { 
       //call another network request here, but don't want to do 
       //another task = URLSession.shared.... again since we are already on an async thread 

      } 
     } 

回答

1
//call another network request here, but don't want to do 
//another task = URLSession.shared.... again since we are already on an async thread 

這是一種誤解。此時創建另一個網絡請求沒有任何問題。所做的就是在適當的隊列上放置另一個請求。無論您在計劃時運行什麼線程,都無關緊要。

我強烈推薦Apple的​​。您可能會對iOS併發如何工作產生誤解。 iOS工作的一個關鍵部分是理解GCD隊列,特別是它們與線程的區別。

相關問題