gpt4 book ai didi

ios - 在 Swift 中,当使用 dataTaskWithRequest 时,completionHandler 在请求超时时返回 nil NSURLResponse

转载 作者:可可西里 更新时间:2023-11-01 01:06:20 24 4
gpt4 key购买 nike

当我将数据发布到服务时,请求通常没有问题,但如果它花费的时间太长并且超时,在完成处理程序中,我得到的 NSURLResponse 为 nil,即使它永远不会返回 nil。

我正在使用 Swift 1.1,下面是我如何执行此操作的示例:

func postX(actionKey:String, postData:AnyObject, callBack:((data:NSData?, resp: NSURLResponse) -> Void)?) -> Void
{
var mreq = createRequest(actionKey, method: "POST", https: true, json: true)
if (self.dataTask != nil)
{
self.dataTask?.cancel()
}
var err: NSError?
mreq.HTTPBody = NSJSONSerialization.dataWithJSONObject(postData, options: nil, error: &err)
self.dataTask = self.getSession().dataTaskWithRequest(mreq, completionHandler: { (data:NSData!, resp: NSURLResponse!, error: NSError!) -> Void in

Dlog.log("response: \(resp)") //Prints out: response: nil
if (error != nil)
{
//do something
}
else
{
//do something else
}
})
if (self.dataTask != nil)
{
self.dataTask!.resume()
}
}

最佳答案

dataTaskWithRequestcompletionHandler block 中的 NSURLResponse 引用是可选的(意味着它可以 )。如果请求超时,您希望它是 nil(因为您可能还没有收到任何响应)。

我建议将 callback 闭包的 NSURLResponse 参数更改为可选参数,就像 dataTaskWithRequest 一样。 (我可能还会返回可选的 NSError,这样您就可以检查特定的错误。)并且您可以通过查找 NSURLErrorTimedOut 来检测超时错误。

例如:

func postX(actionKey: String, postData: AnyObject, callBack: (NSData?, NSURLResponse?, NSError?) -> ()) {
let request = createRequest(actionKey, method: "POST", https: true, json: true)

dataTask?.cancel() // note, we don't need `if` clause, as the `?` does everything for us

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(postData, options: [])

let task = getSession().dataTaskWithRequest(request) { data, response, error in
callBack(data, response, error)
}

task.resume()

dataTask = task
}

然后你可以像这样使用它:

postX(actionKey, postData: postData) { data, response, error in

// handle response however you want

// did it time out?

if error?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut {
print("timed out") // note, `response` is likely `nil` if it timed out
}

}

为了完整起见,Swift 3 实现可能如下所示:

func postX(actionKey: String, postData: AnyObject, callBack: (Data?, URLResponse?, NSError?) -> ()) {
var request = createRequest(actionKey: actionKey, method: "POST", https: true, json: true)

dataTask?.cancel() // note, we don't need `if` clause, as the `?` does everything for us

request.httpBody = try! JSONSerialization.data(withJSONObject: postData, options: [])

let task = getSession().dataTask(with: request) { data, response, error in
callBack(data, response, error)
}

task.resume()

dataTask = task
}

func createRequest(actionKey: String, method: String, https: Bool, json: Bool) -> URLRequest {
var request = URLRequest(url: ...)
...
return request
}

它会被称为:

postX(actionKey: actionKey, postData: postData) { data, response, error in

// handle response however you want

// did it time out?

if error?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut {
print("timed out") // note, `response` is likely `nil` if it timed out
}

}

关于ios - 在 Swift 中,当使用 dataTaskWithRequest 时,completionHandler 在请求超时时返回 nil NSURLResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28569483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com