我在切换应用程序或进入后台之前调用 API,但是当我在 5-10 秒内切换到前台时,API 请求超时。我在 RxSwift 中使用 Moya+Alaomofile。我已经尝试增加 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<MoyaOrder>(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)
}()
}
最佳答案
目前在Moya的Github上找了一圈,没有找到合适的解决方案。 所以为了解决这个问题,我将所有请求保存在一个堆栈中,当应用程序再次激活时,我停止当前请求并再次重新启动它们。
https://stackoverflow.com/questions/62536437/
相关文章:
python - 如何防止vscode/ms-python清除测试结果?
firebase - 使用 Firebase 通过 OAuth2 创建登录名时指定 UserId 格
java - 如何获取 Azure blob 下载中的 blob 下载进度
java - 如何在 Tycho 构建中设置 Java 编译器兼容性?
python - Pandas 的 Mypy/typeshed stub
amazon-web-services - 由于 Amplify 上的身份验证 token 过期较短