解决方法
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)])