【问题标题】:Alamofire how to set timeoutAlamofire如何设置超时
【发布时间】:2019-06-25 23:44:48
【问题描述】:

我正在开发我的第一个 iOS 应用程序并使用 Alamofire 进行网络通话。这很好也很容易。我在单独的类中实现了 Alamofire,我们称之为 WebserviceHelper。现在我在整个应用程序中使用这个类。但现在我想设置超时。

这里是关于我如何在 WebserviceHelper 类中创建函数的 sn-p。

  static func requestService(methodName: String, method: HTTPMethod, parameters: Parameters, headers: HTTPHeaders? = nil, showLoaderFlag: Bool, viewController: UIViewController, completion: @escaping (_ success: JSON) -> Void) {

    let reachability = Reachability()!

    /*
     checking for internet connection
     */
    if(reachability.connection != .none) {

        /*
         Showing Loader here
         */
        if(showLoaderFlag){
        ViewControllerUtils.customActivityIndicatory(viewController.view, startAnimate: true)
        }

        print("Web Service Url - \(Common.URL+methodName)")
        print("Parameters - \(parameters)")

        /*
         Request configure
         */

        Alamofire.request(Common.URL+methodName, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers) .responseJSON { response in

在这里您可以看到它非常易于使用。我使用了很多方法来设置超时,但它对我不起作用。 this 是我尝试过的。还有this 链接,但没有什么对我有用。

你们能帮我用 Alamofire 设置超时吗?

【问题讨论】:

  • Set timeout in Alamofire的可能重复
  • 相关链接在最新版本中无效。请不要将其标记为最新版本
  • Mayur Karmur 是正确的。但是,我还想补充一点,您不应该将可达性用作网络调用的预检查。 Apple 特别反对这种做法,因为可达性并不那么精确。
  • Jon Sheir 我应该用什么来代替可达性

标签: ios swift alamofire


【解决方案1】:

您应该尝试URLRequest,它在最新的 Alamofire 中工作,因为我已经实现了它。见以下代码:

guard let url = URL(string: "") else { return }
var urlRequest = URLRequest(url: url)
urlRequest.timeoutInterval = TimeInterval(exactly: 30)!  // Set timeout in request of 30 seconds

Alamofire.request(urlRequest).response { (dataResponse) in

}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 2018-12-12
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2020-04-15
    • 2017-10-14
    相关资源
    最近更新 更多