在我们的项目中,我们使用网络管理器来使用 Alamofire 处理所有请求和响应。在 alamofire 中,我们默认的内部超时为 30。但是我们需要将某些请求的“timeoutIntervalForRequest”更改为 50,60 等。
private var manager : SessionManager?
func sessionManager() -> SessionManager {
if let manage = self.manager {
return manage
}
else {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
self.manager = SessionManager(configuration: configuration, delegate: CustomSessionDelegate())
return self.manager!
}
}
我添加了参数 timeinterval 并在其中设置了默认值。
func sessionManager(timeoutInterval:Double = 30) -> SessionManager {
//passed the param
configuration.timeoutIntervalForRequest = timeoutInterval
}
但时间间隔值未更新。我的问题是如何使用此函数更新每个请求的 SessionManager 的超时间隔。我们可以使用每个 API 的超时间隔来实现它,但我希望它高于通用功能。
最佳答案
您可以将函数重写为:
func sessionManager(timeInterval : Double) -> SessionManager {
if let manage = self.manager {
return manage
}
else {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = timeInterval
self.manager = SessionManager(configuration: configuration, delegate: CustomSessionDelegate())
return self.manager!
}
}
它在每次 Api 调用中都能正常工作
关于ios - 快速更新了每个 API 的 almofire timeoutIntervalForRequest 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60164208/