Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set timeout for requests using Moya pod?

Tags:

ios

swift

moya

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?

like image 895
Roberto Sampaio Avatar asked Oct 18 '16 18:10

Roberto Sampaio


3 Answers

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)])
like image 82
askielboe Avatar answered Oct 22 '22 16:10

askielboe


@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)
    }()
}
like image 6
Jonathan Cabrera Avatar answered Oct 22 '22 17:10

Jonathan Cabrera


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)
                }
            }
    }
}
like image 4
Angel Martin Avatar answered Oct 22 '22 16:10

Angel Martin