Moya: How to set network timeout?

Created on 27 Oct 2016  Â·  10Comments  Â·  Source: Moya/Moya

The default timeout >60s,I want to set it by myself.
thanks!

question

Most helpful comment

You can set it like as:

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)
    }()    
}

and you should put DefaultAlamofireManager into the your provider like this:

let Provider = MoyaProvider<GithubAPI>(endpointClosure: endpointClosure,
        manager: DefaultAlamofireManager.sharedManager,
        plugins: [NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)])

All 10 comments

You can set it like as:

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)
    }()    
}

and you should put DefaultAlamofireManager into the your provider like this:

let Provider = MoyaProvider<GithubAPI>(endpointClosure: endpointClosure,
        manager: DefaultAlamofireManager.sharedManager,
        plugins: [NetworkActivityPlugin(networkActivityClosure: networkActivityClosure)])

Does @haydarKarkin's answer solve your problem, @zeew? Let us know it doesn't :)

Closing this for now @zeew, please re-open this if @haydarKarkin's solution doesn't solve your problem!

@AndrewSB how To Target special setup?
tks

@mrdaios can you elaborate?

public enum GitHub {
    case zen
    case userProfile(String)
    case userRepositories(String)
    case requestSomeThings
}

Github.requestSomeThings may take a while, how to set timeout for this.

@AndrewSB tks.

You want to have a variable timeout based on the target? For example: you want .zen to timeout in 60 seconds, but .requestSomeThings to timeout after 45 seconds?

yes。

Oh that would be interesting for me too since I'd like to avoid reference Alamofire directly

Hmm, yeah. That's an interesting question. I don't know if we have a per-target timeout setup yet.

The easiest thing right now, would be to add var timeoutInterval: TimeInterval { get } to your enum, and cancel override the provider.request function to cancel the request after the target. timeoutInterval.

To get a better solution for per target timeouts, creating a new issue is probably the best idea, to get other people's opinions 😄

Was this page helpful?
0 / 5 - 0 ratings