我正在使用 Swift 3 和Moya pod。
我使用Basic Usage配置了我需要的一切,但我没有找到任何可以设置超时的函数或变量(对于每个请求或特定请求)。
我怎样才能做到这一点?
haydarKarkin在 GitHub 上的评论中对此提供了答案。下面的代码片段直接从他的评论中复制而来。
您可以通过创建自定义 Alamofire 会话管理器来为 Moya 提供者创建自定义配置:
import Foundation
import Alamofire
class DefaultAlamofireManager: Alamofire.SessionManager {
static let sharedManager: DefaultAlamofireManager = {
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
configuration.timeoutIntervalForRequest = 20 // as seconds, you can set your request timeout
configuration.timeoutIntervalForResource = 20 // as seconds, you can set your resource timeout
configuration.requestCachePolicy = .useProtocolCachePolicy
return DefaultAlamofireManager(configuration: configuration)
}()
}
然后在声明您的 Provider 时包含自定义 Alamofire 管理器:
let Provider = MoyaProvider<GithubAPI>(endpointClosure: endpointClosure,
manager: DefaultAlamofireManager.sharedManager,
plugins: [NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)])
@askielboe 为Alamofire 5更新的最佳答案:
class DefaultAlamofireSession: Alamofire.Session {
static let shared: DefaultAlamofireSession = {
let configuration = URLSessionConfiguration.default
configuration.headers = .default
configuration.timeoutIntervalForRequest = 20 // as seconds, you can set your request timeout
configuration.timeoutIntervalForResource = 20 // as seconds, you can set your resource timeout
configuration.requestCachePolicy = .useProtocolCachePolicy
return DefaultAlamofireSession(configuration: configuration)
}()
}
使用最新的 Moya 和 Alamofire 版本,使用自定义 Alamofire 会话设置提供者的会话参数。
class DefaultAlamofireSession: Alamofire.Session {
static let shared: DefaultAlamofireSession = {
let configuration = URLSessionConfiguration.default
configuration.headers = .default
configuration.timeoutIntervalForRequest = 30 // as seconds, you can set your request timeout
configuration.timeoutIntervalForResource = 30 // as seconds, you can set your resource timeout
configuration.requestCachePolicy = .useProtocolCachePolicy
return DefaultAlamofireSession(configuration: configuration)
}()
}
let provider = MoyaProvider<OpenApi>(session: DefaultAlamofireSession.shared, plugins: [authPlugin])
您可以在会话管理器中管理会话配置并决定使用不同超时值的请求。
import Foundation
import Moya
struct MyNetworkManager {
static let provider = MoyaProvider<MyService>()
static func request(
target: MyService,
success successCallback: @escaping (JSON) -> Void,
error errorCallback: @escaping (Error) -> Void,
failure failureCallback: @escaping (MoyaError) -> Void
) {
//Check request
switch target {
case .quickAnswer:
provider.manager.session.configuration.timeoutIntervalForRequest = 2
default:
provider.manager.session.configuration.timeoutIntervalForRequest = Manager.default.session.configuration.timeoutIntervalForRequest
}
provider.request(target) { result in
let url = target.path
switch result {
case let .success(response):
do {
let _ = try response.filterSuccessfulStatusCodes()
let json = try JSON(response.mapJSON())
successCallback(json)
}
catch {
errorCallback(error)
}
case let .failure(error):
failureCallback(error)
}
}
}
}