网络请求在Swift中不定期地超时(使用Alamofire)

问题描述 投票:0回答:1

我们的一些请求很少偶尔遇到超时问题。很难再现,因为它只是偶然发生并且在重试时(通过执行完全相同的操作重新发送请求,比如按下按钮)我们很可能成功地获得响应。这发生在不同的设备上。

这里我们的平台信息: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处理了我们提出的要求。

这种情况非常随机发生,正如我们所提到的,手动重试大多数都会成功。我们无法从服务器端捕获任何日志。我们对这个问题有一些假设。

  • iOS是错误的,它随机地偶尔丢失请求
  • Alamofire是一种越野车,它偶尔随机丢失请求
  • 我们的网络随机偶尔丢失
  • 我们的服务器无法随机响应

我们还将使用Netfox来窥探来自我们设备的请求,以便我们可以缩小问题范围。

以前有没有人有类似的问题?谢谢!

ios swift networking alamofire promisekit
1个回答
0
投票

有了这样一个问题,没有什么可以是决定性的,但对我来说,一直是网络问题。我没有使用Netfox,但Charles对此非常有帮助。我也使用curl等获得第二意见(帮助你确定你是否疯狂等)。

在这一点上,既然你不能“刺激失败”每个Agan的必读book on debugging,你需要更多的信息。

您最好的选择就是您所提到的 - 使用像Charles这样的代理来查看网络级别发生的事情(至少对于传出请求),并单独向服务器添加日志记录。

© www.soinside.com 2019 - 2024. All rights reserved.