我遇到了一个奇怪的问题,我的 playground 中的 Alamofire.request(.GET) 语句在 playground 延迟一段时间后被执行
设置:我遵循以下 link导入 Alamofire 框架以在 xcode playground 中测试网络请求。
这是我在 Playground 上的代码。当我查看我的网络服务器的日志时,日志会在大约几分钟的延迟后得到更新。我已经确认不是日志进程导致了延迟。使用 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)")
}
}
最佳答案
像网络请求这样的时间延迟的 Playground 行为是......充其量是不可预测的。
尝试让 playground 知道它应该等待您的网络请求:
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()
}
关于xcode - 在 Playground 上使用 Alamofire 延迟发出 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535968/