【问题标题】:Moya+Alamofire POST request is getting timeout when switching between apps or going into background在应用程序之间切换或进入后台时,Moya+Alamofire POST 请求超时
【发布时间】:2020-10-13 14:43:57
【问题描述】:

我在切换应用程序或进入后台之前调用 API,但是当我在 5-10 秒内切换到前台时,API 请求会超时。我正在将 Moya+Alaomofile 与 RxSwift 一起使用。我尝试增加 SessionConfiguration 超时,但没有帮助,API 在 Android 和 Web 上运行良好。以下是我正在使用的代码:

APIOrder.provider.rx.request(.placeOrder(orderDetails: orderDetails))
            .map { any -> SROrderData in
                do {
                    let decoder = JSONDecoder()
                    guard let paymentDataModel = try decoder.decode(Safe<SRPaymentDataResponse>.self, from: any.data).value else {
                        throw ApiError.parseError
                    }
                    return paymentDataModel.data
                } catch {
                    throw try throwAPIError(data: any.data)
                }
        }

static var provider = MoyaProvider&lt;MoyaOrder&gt;(manager: DefaultAlamofireManager.sharedManager)

class DefaultAlamofireManager: Alamofire.SessionManager {
    static let sharedManager: DefaultAlamofireManager = {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 120 // as seconds, you can set your request timeout
        configuration.timeoutIntervalForResource = 120 // as seconds, you can set your resource timeout
        configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        configuration.shouldUseExtendedBackgroundIdleMode = true
        if #available(iOS 13.0, *) {
            configuration.allowsExpensiveNetworkAccess = true
        }
        configuration.networkServiceType = .background
        configuration.waitsForConnectivity = true
        return DefaultAlamofireManager(configuration: configuration)
    }()
}

【问题讨论】:

    标签: ios swift alamofire rx-swift moya


    【解决方案1】:

    目前我在 Moya Github 上找了一圈,但没有找到合适的解决方案。 所以为了解决这个问题,我将所有请求保存在一个堆栈中,当应用程序再次处于活动状态时,我会停止当前请求并重新启动它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多