答案 0 :(得分:24)
haydarKarkin在comment on 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)
}()
}
然后在声明您的提供商时包含自定义Alamofire Manager:
let Provider = MoyaProvider<GithubAPI>(endpointClosure: endpointClosure,
manager: DefaultAlamofireManager.sharedManager,
plugins: [NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)])
答案 1 :(得分:1)
您可以在会话管理器中管理会话配置,并确定使用不同超时值的请求。
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)
}
}
}
}
答案 2 :(得分:1)
使用最新的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])
答案 3 :(得分:0)
@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)
}()
}