2015-11-05 54 views
0

我遇到了一個奇怪的問題,即在我的操場Alamofire.request(不用彷徨)語句被一些延遲後在操場延遲使得在操場上用Alamofire

設置執行HTTP請求:我跟着​​下面link導入Alamofire框架來測試xcode操場中的聯網請求。

這是我在我的操場上的代碼。當我查看我的網絡服務器的日誌時,日誌會在延遲幾分鐘後得到更新。我已經證實,這不是導致延遲的日誌過程。使用curl和瀏覽器進行相同的http請求,我看到日誌幾乎立即得到更新。

import UIKit 

    import Alamofire 



    Alamofire.request(.GET, "http://localhost:5010/asdf") 
     .responseJSON { response in 
      print ("Hello there in playground") 
      print(response.request) // original URL request 
      print(response.response) // URL response 
      print(response.data)  // server data 
      print(response.result) // result of response serialization 

      if let JSON = response.result.value { 
       print("JSON: \(JSON)") 
      } 
    } 

回答

2

對於網絡請求等時間延遲的事情,遊樂場行爲至多是......不可預知的。

嘗試讓操場知道它應該等待您的網絡請求:

import UIKit 
import Alamofire 

import XCPlayground 

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true 

Alamofire.request(.GET, "http://localhost:5010/asdf") 
    .responseJSON { response in 
     print ("Hello there in playground") 
     print(response.request) // original URL request 
     print(response.response) // URL response 
     print(response.data)  // server data 
     print(response.result) // result of response serialization 

     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
     } 

     XCPlaygroundPage.currentPage.finishExecution() 
    }