gpt4 book ai didi

ios - NSURLSession dataTaskWithRequest 只在第一次工作

转载 作者:行者123 更新时间:2023-11-28 06:34:19 24 4
gpt4 key购买 nike

我目前正在像往常一样开发一个 WS 调用,但我不知道为什么这只在我第一次调用它时有效,数据第一次有值(value),但如果我再次调用它,它返回 nil。如果我查看响应,我可以看到它在数据为 nil 时返回 412 值。

// Request
var endPoint = Constants.CONFIG.WS_LANZADERA

if Utils.getEnviroment() == Constants.Environment.Develop.rawValue {
endPoint = endPoint + Constants.WS.SHIP
}

let request = NSMutableURLRequest(URL: NSURL(string: endPoint)!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: Constants.REQUEST.TIMEOUT)

request.HTTPMethod = Constants.HTTP.METHOD.POST
request.HTTPBody = shipParams.getDictionary().dataFromHttpParameters()

let session = NSURLSession.sharedSession()

// Handler

let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in

if error == nil {
let jsonResult = JSON(data: data!)
if jsonResult["error"] != "" {
//.arrayObject
let proxaXMLData = jsonResult["ProsaXMLData"]
let SPM = proxaXMLData[3]
//let userList :Array<User> = Mapper<User>().mapArray(dict)!
//let user = userList[0]
self.webServiceDelegates.forEach({ (delegate) -> () in
if (delegate.respondsToSelector(#selector(WebServiceDelegate.responseShipListSuccess))) {
delegate.responseShipListSuccess?()
}
})
} else {
self.webServiceDelegates.forEach({ (delegate) -> () in
if (delegate.respondsToSelector(#selector(WebServiceDelegate.responseShipListFailed(_:message:)))) {
delegate.responseShipListFailed!(jsonResult["error"]["code"].string!, message: jsonResult["error"]["message"].string!)
}
})
}

}
else {
self.webServiceDelegates.forEach({ (delegate) -> () in
if (delegate.respondsToSelector(#selector(WebServiceDelegate.responseShipListFailed(_:message:)))) {
delegate.responseShipListFailed!(error!.code.description, message: error!.localizedDescription)
}
})
}
})

dataTask.resume()

有人知道吗?

最佳答案

通过更改缓存策略解决

let request = NSMutableURLRequest(URL: NSURL(string: endPoint)!,
cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: Constants.REQUEST.TIMEOUT)

关于ios - NSURLSession dataTaskWithRequest 只在第一次工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39488260/

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