I'm using Swift 3 and the Moya pod.
I configured everything I needed using the Basic Usage, but I didn't find any function or variable that I can set the timeout (for every requests or for a specific request).
How can I do this?
haydarKarkin has provided an answer to this in a comment on GitHub. The code snippets below are copied directly from his comment.
You can create a custom configuration for a Moya provider by creating a custom Alamofire Session Manager:
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)
}()
}
Then include the custom Alamofire Manager when declaring your Provider:
let Provider = MoyaProvider<GithubAPI>(endpointClosure: endpointClosure,
manager: DefaultAlamofireManager.sharedManager,
plugins: [NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)])
@askielboe's great answer updated for 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)
}()
}
You can Manage the Session Configuration in your session Manager and decide the requests that use a different timeout value.
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)
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With