class APIClient {
var user = User()
let alamoFireManager : Alamofire.Manager?
let center = NSNotificationCenter.defaultCenter()
init(){
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 4 // seconds
configuration.timeoutIntervalForResource = 4
self.alamoFireManager = Alamofire.Manager(configuration: configuration)
}
func test(){
//This does not respect the 4 second time out. Why?
self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
(req, res, json, error) in
if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
}
}
}
答案 0 :(得分:37)
您确定没有强制执行4秒超时吗?我创建了一个实验:
let center = NSNotificationCenter.defaultCenter()
var alamoFireManager : Alamofire.Manager?
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 4 // seconds
configuration.timeoutIntervalForResource = 4
self.alamoFireManager = Alamofire.Manager(configuration: configuration)
self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/2", parameters: nil).responseJSON {
(req, res, json, error) in
println("First json \(json)")
println("First error \(error)")
}
self.alamoFireManager!.request(.POST, "http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6", parameters: nil).responseJSON {
(req, res, json, error) in
println("Second \(json)")
println("Second \(error)")
}
我得到了控制台输出:
First json Optional({
name = timeoutTest;
value = 2;
})
First error nil
Second nil
Second Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7f91dc8223e0 {NSErrorFailingURLKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://oznet.go.ro/iDorMobile/ConfigServer/api.php/timeout/6})
我的示例中的网址目前已关闭,我将尝试将其重新联机。 您可以使用示例中的网址进行测试。通过在末尾设置不同的数字,您可以修改超时(以秒为单位)。
我使用过cocoapods Alamofire的最新版本。
答案 1 :(得分:9)
您需要为请求管理器创建一个全局变量:
var alamoFireManager = Alamofire.Manager.sharedInstance
配置自定义参数后:
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 4 // seconds
configuration.timeoutIntervalForResource = 4
self.alamoFireManager = Alamofire.Manager(configuration: configuration)
答案 2 :(得分:8)
这是Swift 3.0 / Alamofire 4.0代码,用于获取具有5秒超时的alamofireManager:
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForResource = 5 // seconds
let alamofireManager = Alamofire.SessionManager(configuration: configuration)
答案 3 :(得分:6)
我想这个适用于您,请注意您必须在函数外声明 Alamofire.Manager 的实例,请参阅下面的代码
//Declare it out side the function
var alamoFireManager : Alamofire.Manager!
func callUrl() {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForResource = 10800 // seconds
configuration.timeoutIntervalForRequest = 10800 // seconds
alamoFireManager = Alamofire.Manager(configuration: configuration)
alamoFireManager.request(.POST, url, parameters: dir as? [String : AnyObject], encoding: .JSON, headers: [ "Content-Type": "application/json"])
.responseJSON { response in
}
}
希望这可以帮助你
答案 4 :(得分:3)
init(){
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForResource = 4
configuration.timeoutIntervalForRequest = 4
self.alamoFireManager = Alamofire.Manager(configuration: configuration)
self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
(req, res, json, error) in
if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
}
}
}
在同一个init函数中使用self.alamoFireManager!.request
,超时延迟仅适用于不是整个函数的请求。可能会有所帮助。
答案 5 :(得分:1)
当我使用上面的方法时,我得到了NSURLErrorDomain。 这是我的代码(swift 3.2 / Alamofire 4.4)
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 4
manager.request(.POST, CONSTANTS.APIEndpoint+"/test", parameters: parameters).responseJSON {
(req, res, json, error) in
if let json = self.handleAPIResponse(req, res: res, json_data: json, error: error){
}