我们的一些请求很少偶尔遇到超时问题。很难再现,因为它只是偶然发生并且在重试时(通过执行完全相同的操作重新发送请求,比如按下按钮)我们很可能成功地获得响应。这发生在不同的设备上。
这里我们的平台信息:XCode 10.1 Swift 4.2 Alamofire 4.8.1 PromiseKit 6.8.3
这是我们的一个请求示例:
public class NetworkingHelpers {
// MARK: Singleton
public static let shared = NetworkingHelpers()
private let alamofireManager: SessionManager
// MARK: Init
private init() {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 30
alamofireManager = Alamofire.SessionManager(configuration: configuration)
}
public func makePublicRequest<T: Decodable>(url: String, decodeAsType: T.Type, method: HTTPMethod = .get,
params: [String: Any]? = nil) -> Promise<T> {
return alamofireManager.request(url, method: method, parameters: params, encoding: JSONEncoding.default, headers: self.anonymousHeaders)
.response(.promise)
.recover(policy: .allErrorsExceptCancellation) { error -> Promise<(URLRequest, HTTPURLResponse, Data)> in
// This is a error thrown by the alamofireManager, but not from the server
// Most likely it is a timeout
let e = error as NSError
throw RequestErrorUtils.generateRequestError(e.code)
}
.then { (request, response, data) -> Promise<T> in
// Process response
}
}
如果超时,Alamofire
向我们抛出错误-1001
代码,这是30秒超时错误。这也意味着alamofireManager
处理了我们提出的要求。
这种情况非常随机发生,正如我们所提到的,手动重试大多数都会成功。我们无法从服务器端捕获任何日志。我们对这个问题有一些假设。
我们还将使用Netfox
来窥探来自我们设备的请求,以便我们可以缩小问题范围。
以前有没有人有类似的问题?谢谢!
有了这样一个问题,没有什么可以是决定性的,但对我来说,一直是网络问题。我没有使用Netfox,但Charles对此非常有帮助。我也使用curl等获得第二意见(帮助你确定你是否疯狂等)。
在这一点上,既然你不能“刺激失败”每个Agan的必读book on debugging,你需要更多的信息。
您最好的选择就是您所提到的 - 使用像Charles这样的代理来查看网络级别发生的事情(至少对于传出请求),并单独向服务器添加日志记录。