0

我正在使用 NSURLSessionDataTask 从打开由 iPhone 连接的 adhoc-WiFi 的设备获取 JSON 数据。(连接通过 TCP 运行)。

该设备非常慢 - 发送特定响应大约需要 5 秒。通过dataTask.resume()我的应用程序触发请求后,只需等待调用 completionHandler 即可。不幸的是,请求被一次又一次地触发(每次延迟约 1 秒)。我猜这是由 dataTask 或 NSURLSession 发起的。我可以在调试设备时监控重复的请求。

这是我正在使用的代码:

let URL = NSURL(string: "<myURL>")! 

let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
configuration.HTTPShouldUsePipelining = false
configuration.HTTPMaximumConnectionsPerHost = 1
configuration.allowsCellularAccess = false
configuration.timeoutIntervalForRequest = 30

let session = NSURLSession(configuration: configuration)
let dataTask = session.dataTaskWithURL(URL) {
// the request gets fired repeatedly before this completionHandler is called!
(data, response, error) -> Void in
...
}

dataTask.resume()

有谁知道如何防止 iOS 重新触发这些请求?

4

0 回答 0