Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set network timeout? #743

Closed
zeew opened this issue Oct 27, 2016 · 10 comments
Closed

How to set network timeout? #743

zeew opened this issue Oct 27, 2016 · 10 comments
Labels

Comments

@zeew
Copy link

zeew commented Oct 27, 2016

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

@loplopLover
Copy link

loplopLover commented Nov 5, 2016

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)])

@BasThomas
Copy link
Contributor

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

@AndrewSB
Copy link
Member

AndrewSB commented Dec 8, 2016

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

@AndrewSB AndrewSB closed this as completed Dec 8, 2016
@mrdaios
Copy link

mrdaios commented Dec 14, 2016

@AndrewSB how To Target special setup?
tks

@AndrewSB
Copy link
Member

@mrdaios can you elaborate?

@AndrewSB AndrewSB reopened this Dec 14, 2016
@mrdaios
Copy link

mrdaios commented Dec 15, 2016

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.

@AndrewSB
Copy link
Member

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?

@mrdaios
Copy link

mrdaios commented Dec 21, 2016

yes。

@Blackjacx
Copy link

Blackjacx commented Apr 10, 2017

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

@AndrewSB
Copy link
Member

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 😄

@Moya Moya locked and limited conversation to collaborators Apr 10, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants