Skip to content

Instantly share code, notes, and snippets.

@SergLam
Created January 30, 2020 15:14
Show Gist options
  • Save SergLam/1993d2f3b303286de13c2eaaf0417070 to your computer and use it in GitHub Desktop.
Save SergLam/1993d2f3b303286de13c2eaaf0417070 to your computer and use it in GitHub Desktop.
Alamofire - custom session manager
import Alamofire
import Foundation
final class CustomAlamofireManager: Alamofire.SessionManager {
private static let requestTimeOutTimeInterval: TimeInterval = {
#if DEBUG
return 30.0
#else
return 120.0
#endif
}()
static let sharedManager: CustomAlamofireManager = {
// NOTE: Background configuration is required to handle
// network requests correctly while app come foreground / background
let configuration = URLSessionConfiguration.background(withIdentifier: "com.bundle_id")
configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
configuration.timeoutIntervalForRequest = requestTimeOutTimeInterval
configuration.timeoutIntervalForResource = requestTimeOutTimeInterval
configuration.requestCachePolicy = .useProtocolCachePolicy
return CustomAlamofireManager(configuration: configuration)
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment