How to execute alamofire background upload request? - swift

I need to send zip file to server side.
There is my request which I need to work in background
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 10 // seconds
alamoFireManager = Alamofire.SessionManager(configuration: configuration)
appDeligate.log.debug("request was sended")
alamoFireManager.upload(deligate.data!,
to: deligate.url,
method: .post,
headers: headers)
.uploadProgress(closure: {
progress in
print("Upload Progress: \(progress.fractionCompleted)")
})
.validate()
.responseJSON {}
Now it is work properly, but I need to execute this in background. According to the Alamofire README doc
https://github.com/Alamofire/Alamofire
it says
Creating a Session Manager with Background Configuration
let configuration = URLSessionConfiguration.background(withIdentifier: "com.example.app.background")
let sessionManager = Alamofire.SessionManager(configuration: configuration)
I changed my configuration to correspond background configuration
now it looks like this
let configuration = URLSessionConfiguration.background(withIdentifier: "com.room.myApp")
configuration.timeoutIntervalForRequest = 10 // seconds
alamoFireManager = Alamofire.SessionManager(configuration: configuration)
alamoFireManager.upload(deligate.data!,
to: deligate.url,
method: .post,
headers: headers)
.uploadProgress(closure: {
progress in
print("Upload Progress: \(progress.fractionCompleted)")
})
.validate()
.responseJSON {}
And I get error
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks from NSData are not supported in background sessions.'
*** First throw call stack:
(0x18ec511b8 0x18d68855c 0x18f33808c 0x18f33796c 0x18f336e68 0x100ef9218 0x100f05dc8 0x18f336dc8 0x18f3378cc 0x100255890 0x1002518e8 0x100234200 0x100234448 0x100ef9218 0x100f05dc8 0x100233fc4 0x100255290 0x10029d238 0x10029ae4c 0x10029ac34 0x10006dd78 0x100071044 0x100082708 0x10002b310 0x100ef9258 0x100ef9218 0x100f0726c 0x100f08e2c 0x100f08b78 0x18dce32a0 0x18dce2d8c)
libc++abi.dylib: terminating with uncaught exception of type NSException
What am I doing wrong?
Is it issue from my side or lib side?
Feel free to ask
Edit
there is sending flow
There is how I create zip file
internal func madeRequest() {
DispatchQueue.global().async {
if self.createZipPhotoDir() {
self.appDeligate.log.debug("zip dir was created")
self.serverConnection.makeServerRequest()
} else {
self.appDeligate.log.error("can NOT execute createZipPhotoDir()")
}
}
}
private func createZipPhotoDir() -> Bool {
let withContentsOfDirectory: String! = UtilDirectory.pathToMyCurrentAvatarDir.tweak() // "/var/mobile/Containers/Data/Application/739A895E-7BCA-47A8-911F-70FBC812CEB3/Documents/default#domein.com/AvatarPackage/name/"
let atPath: String! = UtilDirectory.tempZipPath.tweak() // "/var/mobile/Containers/Data/Application/739A895E-7BCA-47A8-911F-70FBC812CEB3/Documents/default#domein.com/tmpZipDir.zip"
return SSZipArchive.createZipFile(atPath: atPath, withContentsOfDirectory: withContentsOfDirectory)
}
zip file is creating ok
Then I make server request
required init() {
configureAlamofireManager()
}
private func configureAlamofireManager() {
let configuration = URLSessionConfiguration.background(withIdentifier: "com.fittingroom.newtimezone.Fitzz")
alamoFireManager = Alamofire.SessionManager(configuration: configuration)
}
internal func makeServerRequest() {
appDeligate.log.debug("request was sended")
alamoFireManager.upload(deligate.data!,
to: deligate.url,
method: .post,
headers: headers)
.uploadProgress(closure: {
progress in
print("Upload Progress: \(progress.fractionCompleted)")
})
.validate()
.responseJSON {
[weak self]
response in
self?.appDeligate.log.debug("response : \(response)")
self?.appDeligate.log.debug(String(describing: response.timeline))
switch response.result {
case .success(let value):
self?.appDeligate.log.debug("succes in server connection response")
let result = self?.getStatusCodeAndData(json: JSON(value))
self?.deligate.onSuccess(statusCode: result?.statusCode, data: result?.data)
case .failure(let error):
self?.appDeligate.log.error("error in UploadingRequest : \(error)")
self?.deligate.onError()
}
}
}
There is a way how I get data to send
internal var data: Data {
var data = Data()
let filePath = UtilDirectory.tempZipPath.tweak()
if let result = UtilFile.exists(path: filePath), result.isFileExist == true, result.whatIsIt == .file {
if let fileData = FileManager.default.contents(atPath: filePath) {
data = fileData
appDeligate.log.debug("*** DATA : \(data) ***")
} else {
print("Could not parse the file")
}
} else {
appDeligate.log.error("some ERROR here: file not exist")
}
return data
}

from Background Transfer Considerations
:
Only upload tasks from a file are supported (uploading from data objects or a stream will fail after the program exits).
that means it is limitation from NSURLSession - you need you upload from a file and then try to solve the other error with file
Update
appDeligate.log.debug("request was sended")
let tempZipFilePath = UtilDirectory.tempZipPath.tweak()
alamoFireManager.upload(tempZipFilePath,
to: deligate.url,
method: .post,
headers: headers)

did you see this section Open Radars:
Open Radars
The following radars have some effect on the current implementation of Alamofire.
rdar://26870455 - Background URL Session Configurations do not work in the simulator

Use below code once, Its working for me
import Alamofire
var sessionManager: Alamofire.SessionManager
var backgroundSessionManager: Alamofire.SessionManager
self.sessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.default)
self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.youApp.identifier.backgroundtransfer"))
backgroundSessionManager.upload(multipartFormData: blockFormData!, usingThreshold: UInt64.init(), to: url, method: .post, headers: APIManager.headers(), encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress {
(progress) in
let p = progress.fractionCompleted * 100
uploadProgress(p)
}
upload.responseJSON { response in
switch(response.result) {
case .success(let JSON):
DispatchQueue.main.async {
print(JSON)
}
case .failure(let error):
DispatchQueue.main.async {
print(error)
}
}
}
case .failure(let error):
DispatchQueue.main.async {
print(error)
}
}
})

Related

Alamofire and downloading images from server

I need to download image as data from the URL and recreate it to UIImage(data:). The problem is, that Alamofire downloads only a small amount of image data on first request:
after I make new call to my API, whole image is downloaded:
It really doesn't make any sense to me why it could be like that. The first request always fails to download all the data. This is the code what I'm using to download the data from URL:
func requestWithData(
_ url: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
decoder: JSONDecoder = JSONDecoder(),
headers: HTTPHeaders? = nil,
interceptor: RequestInterceptor? = nil
) -> Future<Data, ServerError> {
return Future({ promise in
AF.request(
url,
method: method,
parameters: parameters,
encoding: JSONEncoding.default,
headers: headers,
interceptor: interceptor ?? self
)
.validate(statusCode: [200, 201, 204, 400, 401])
.responseData(completionHandler: { (response) in
switch response.result {
case .success(let value):
promise(.success(value))
case .failure(let error):
promise(.failure(self.createError(response: response.response, error: error, data: response.data)))
}
})
})
}
func getLocationImage(location: Location) -> Future<Void, ServerError> {
Future { promise in
self.networkManager.requestWithData(Endpoint.locationBadge(locationId: location.id, uuid: location.uuid ?? "").url)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(error))
}
} receiveValue: { [unowned self] imageData in
self.updateLocationImage(location: location, image: imageData)
.sink { completion in
if case .failure(let error) = completion {
promise(.failure(.init(message: "Error while saving the data", code: .coreData, args: [error.localizedDescription])))
}
} receiveValue: { _ in
promise(.success(()))
}
.store(in: &subscription)
}
.store(in: &self.subscription)
}
}
SOLVED: The problem was with Alamofire request, I was able to resolve the issue with using AlamofireImage framework.

how to set timeout in alamofire with DataRequest

let api = request(url, method: .post, parameters:params, headers:nil)
api.responseJSON { (responseData) -> Void in
//code
}
I don't have time to do code refactoring.
So I want to set the timeout based on the code above, how can I do it?
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 12
configuration.timeoutIntervalForResource = 12
let alamoFireManager = Alamofire.Session.init(configuration: configuration)
alamoFireManager.request(url, method: .post, parameters: params).responseJSON { response in
switch (response.result) {
case .success:
break
case .failure(let error):
print(error)
break
}
}

Alamofire session manager crash randomly,swift

I am using Alamofire in my project but sometimes I have seen some non replicable crash in alamofire session manager in alamofire implementation file. As because it is non replicable causes some random crashes in my application.
After lots of debugging I found one log that it might a network issue. If anyone can find anything same please help to solve this. Thanks in advance
open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String:String]) -> DataRequest {
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
}
override open func execute(_ completion: #escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
let managerId:String = UUID().uuidString
// Create a new manager for each request to customize its request header
let manager = createSessionManager()
managerStore[managerId] = manager
let encoding:ParameterEncoding = isBody ? JSONDataEncoding() : URLEncoding()
let xMethod = Alamofire.HTTPMethod(rawValue: method)
let fileKeys = parameters == nil ? [] : parameters!.filter { $1 is NSURL }
.map { $0.0 }
// the status code
if fileKeys.count > 0 {
manager.upload(multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
case let fileURL as URL:
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
}
else {
mpForm.append(fileURL, withName: k)
}
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
case let number as NSNumber:
mpForm.append(number.stringValue.data(using: String.Encoding.utf8)!, withName: k)
default:
fatalError("Unprocessable value \(v) with key \(k)")
}
}
}, to: URLString, method: xMethod!, headers: nil, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
if let onProgressReady = self.onProgressReady {
onProgressReady(upload.uploadProgress)
}
self.processRequest(request: upload, managerId, completion)
case .failure(let encodingError):
var statusCode = encodingResult
if let error = encodingError as? AFError {
// statusCode = error._code // statusCode private
switch error {
case .invalidURL(let url):
print("Invalid URL: \(url) - \(error.localizedDescription)")
case .parameterEncodingFailed(let reason):
print("Parameter encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .multipartEncodingFailed(let reason):
print("Multipart encoding failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
case .responseValidationFailed(let reason):
print("Response validation failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
switch reason {
case .dataFileNil, .dataFileReadFailed:
print("Downloaded file could not be read")
case .missingContentType(let acceptableContentTypes):
print("Content Type Missing: \(acceptableContentTypes)")
case .unacceptableContentType(let acceptableContentTypes, let responseContentType):
print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)")
case .unacceptableStatusCode(let code):
print("Response status code was unacceptable: \(code)")
// statusCode = code
}
case .responseSerializationFailed(let reason):
print("Response serialization failed: \(error.localizedDescription)")
print("Failure Reason: \(reason)")
// statusCode = 3840 ???? maybe..
}
print("Underlying error: \(error.underlyingError)")
} else if let error = encodingError as? AFError {
print("URLError occurred: \(error)")
} else {
print("Unknown error: \(encodingError as? AFError)")
}
print(statusCode)
completion(nil, ErrorResponse.error(415, nil, encodingError))
}
})
} else {
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding, headers: headers)
if let onProgressReady = self.onProgressReady {
onProgressReady(request.progress)
}
processRequest(request: request, managerId, completion)
}
}
EXC_BAD_ACCESS

How to handle many API calls with Swift 3 GCD

I am building an swift app to interact with an MDM API to do large numbers of updates via PUT commands, and I am running in to issues with how to handle the massive numbers of API calls without overloading the servers.
I am parsing through a CSV, and each line is an update. If I run the commands asynchronously, it generates and sends ALL of the API calls immediately, which the server doesn't like.
But if I run the commands synchronously, it freezes my GUI which is less than ideal, as the end user doesn't know what's going on, how long is left, if things are failing, etc.
I have also tried creating my own NSOperation queue and setting the max number of items to like 5, and then putting the synchronous function in there, but that doesn't seem to work very well either. It still freezes the GUI with some really random UI updates that seem buggy at best.
The servers can handle 5-10 requests at a time, but these CSV files can be upwards of 5,000 lines sometimes.
So how can I limit the number of simultaneous PUT requests going out in my loop, while not having the GUI freeze on me? To be honest, I don't even really care if the end user can interact with the GUI while it's running, I just want to be able to provide feedback on the lines that have run so far.
I have a wrapper which a colleague wrote most of, and the async function looks like this:
func sendRequest(endpoint: String, method: HTTPMethod, base64credentials: String, dataType: DataType, body: Data?, queue: DispatchQueue, handler: #escaping (Response)->Swift.Void) {
let url = self.resourceURL.appendingPathComponent(endpoint)
var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 30.0)
request.httpMethod = "\(method)"
var headers = ["Authorization": "Basic \(base64credentials)"]
switch dataType {
case .json:
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
if let obj = body {
do {
request.httpBody = try JSONSerialization.data(withJSONObject: obj, options: JSONSerialization.WritingOptions(rawValue: 0))
} catch {
queue.async {
handler(.badRequest)
}
return
}
}
case .xml:
headers["Content-Type"] = "application/xml"
headers["Accept"] = "application/xml"
request.httpBody = body
/*if let obj = body {
request.httpBody = (obj as! XMLDocument).xmlData
}*/
}
request.allHTTPHeaderFields = headers
session.dataTask(with: request) {
var response: Response
if let error = $2 {
response = .error(error)
} else {
let httpResponse = $1 as! HTTPURLResponse
switch httpResponse.statusCode {
case 200..<299:
if let object = try? JSONSerialization.jsonObject(with: $0!, options: JSONSerialization.ReadingOptions(rawValue: 0)) {
response = .json(object)
} else if let object = try? XMLDocument(data: $0!, options: 0) {
response = .xml(object)
} else {
response = .success
}
default:
response = .httpCode(httpResponse.statusCode)
}
}
queue.async {
handler(response)
}
}.resume()
Then, there is a synchronous option which uses semaphore, which looks like this:
func sendRequestAndWait(endpoint: String, method: HTTPMethod, base64credentials: String, dataType: DataType, body: Data?) -> Response {
var response: Response!
let semephore = DispatchSemaphore(value: 0)
sendRequest(endpoint: endpoint, method: method, base64credentials: base64credentials, dataType: dataType, body: body, queue: DispatchQueue.global(qos: .default)) {
response = $0
semephore.signal()
}
semephore.wait()
return response
}
Usage information is as follows:
class ViewController: NSViewController {
let client = JSSClient(urlString: "https://my.mdm.server:8443/", allowUntrusted: true)
let credentials = JSSClient.Credentials(username: "admin", password: "ObviouslyNotReal")
func asynchronousRequestExample() {
print("Sending asynchronous request")
client.sendRequest(endpoint: "computers", method: .get, credentials: credentials, dataType: .xml, body: nil, queue: DispatchQueue.main) { (response) in
print("Response recieved")
switch response {
case .badRequest:
print("Bad request")
case .error(let error):
print("Receieved error:\n\(error)")
case .httpCode(let code):
print("Request failed with http status code \(code)")
case .json(let json):
print("Received JSON response:\n\(json)")
case .success:
print("Success with empty response")
case .xml(let xml):
print("Received XML response:\n\(xml.xmlString(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue)))")
}
print("Completed")
}
print("Request sent")
}
func synchronousRequestExample() {
print("Sending synchronous request")
let response = client.sendRequestAndWait(endpoint: "computers", method: .get,credentials: credentials, dataType: .json, body: nil)
print("Response recieved")
switch response {
case .badRequest:
print("Bad request")
case .error(let error):
print("Receieved error:\n\(error)")
case .httpCode(let code):
print("Request failed with http status code \(code)")
case .json(let json):
print("Received JSON response:\n\(json)")
case .success:
print("Success with empty response")
case .xml(let xml):
print("Received XML response:\n\(xml.xmlString(withOptions: Int(XMLNode.Options.nodePrettyPrint.rawValue)))")
}
print("Completed")
}
override func viewDidAppear() {
super.viewDidAppear()
synchronousRequestExample()
asynchronousRequestExample()
}
I have modified the send functions slightly, so that they take base64 encoded credentials off the bat, and maybe one or two other things.
Can't you just chain operations to send 3/4 requests at a time per operation?
https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift
Just so you know, NSOperation (also abstracted by Operation with Swift3) are running by default on background threads. Just be careful to not run heavy tasks in your completion block that might run tasks on the main thread (this will freeze your UI).
The only other case I see that can freeze your UI is by executing too many operations at once.
Well, I think I got this covered! I decided to climb out of the rabbit hole a ways and simplify things. I wrote my own session instead of relying on the wrapper, and set up semaphores in it, threw it in an OperationQueue and it seems to be working perfectly.
This was the video I followed to set up my simplified semaphores request. https://www.youtube.com/watch?v=j4k8sN8WdaM
I'll have to tweak the below code to be a PUT instead of the GET I've been using for testing, but that part is easy.
//print (row[0])
let myOpQueue = OperationQueue()
myOpQueue.maxConcurrentOperationCount = 3
let semaphore = DispatchSemaphore(value: 0)
var i = 0
while i < 10 {
let myURL = NSURL(string: "https://my.server.com/APIResources/computers/id/\(i)")
myOpQueue.addOperation {
let request = NSMutableURLRequest(url: myURL! as URL)
request.httpMethod = "GET"
let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = ["Authorization" : "Basic 123456789ABCDEFG=", "Content-Type" : "text/xml", "Accept" : "text/xml"]
let session = Foundation.URLSession(configuration: configuration)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) -> Void in
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
semaphore.signal()
self.lblLine.stringValue = "\(i)"
self.appendLogString(stringToAppend: "\(httpResponse.statusCode)")
print(myURL!)
}
if error == nil {
print("No Errors")
print("")
} else {
print(error!)
}
})
task.resume()
semaphore.wait()
}
i += 1
}

Handle timeout with Alamofire

Is it possible to add timeout handler for Alamofire request?
In my project I use Alamofire this way:
init() {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.timeoutIntervalForRequest = 30
self.alamofireManager = Alamofire.Manager(configuration: configuration)
}
func requestAuthorizationWithEmail(email:NSString, password:NSString, completion: (result: RequestResult) -> Void) {
self.alamofireManager!.request(.POST, "myURL", parameters:["email": email, "password":password])
.responseJSON { response in
switch response.result {
case .Success(let JSON):
//do json stuff
case .Failure(let error):
print("\n\nAuth request failed with error:\n \(error)")
completion(result: .ConnectionFailed)
}
}
}
EDIT:
request fail message
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x7fc10b937320 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=url, NSErrorFailingURLKey=url, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
You can compare error._code and if it is equal to -1001 which is NSURLErrorTimedOut then you know this was a timeout.
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 120
manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"])
.responseJSON {
response in
switch (response.result) {
case .success: // succes path
case .failure(let error):
if error._code == NSURLErrorTimedOut {
print("Request timeout!")
}
}
}
Swift 3
The accepted answer didn't work for me.
After a lot of research, I did it like this:
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 120
manager.request("yourUrl", method: .post, parameters: ["parameterKey": "value"])
Swift 3, Alamofire 4.5.0
I wanted to set the same timeout for every HTTP call in my project.
The key idea is to declare the Alamofire Session Manager as a global variable. Then to create a URLSessionConfiguration variable, set its timeout in seconds and assign it to the manager.
Every call in the project can use this configured session manager.
In my case the global Alamofire Session Manager variable was set in AppDelegate file (globally) and its configuration was managed in its didFinishLaunchingWithOptions method
AppDelegate.swift
import UIKit
var AFManager = SessionManager()
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 4 // seconds
configuration.timeoutIntervalForResource = 4 //seconds
AFManager = Alamofire.SessionManager(configuration: configuration)
return true
}
...
}
From now the Alamofire request function can be called from any part of the app using the afManager.
For example:
AFManager.request("yourURL", method: .post, parameters: parameters, encoding: JSONEncoding.default).validate().responseJSON { response in
...
}
Swift 3.x
class NetworkHelper {
static let shared = NetworkHelper()
var manager: SessionManager {
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 10
return manager
}
func postJSONData( withParams parameters: Dictionary<String, Any>, toUrl urlString: String, completion: #escaping (_ error: Error,_ responseBody: Dictionary<String, AnyObject>?)->()) {
manager.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
if let error = response.result.error {
if error._code == NSURLErrorTimedOut {
print("Time out occurs!")
}
}
}
}
}
Swift 5, Alamofire 5
The cleanest way I found, that works with the latest version of Alamofire is the following:
AF.request(url).response { (dataResponse: AFDataResponse<Data?>) in
switch dataResponse.result {
case .success(let data):
// succes path
case .failure(let error):
switch error {
case .sessionTaskFailed(URLError.timedOut):
print("Request timeout!")
default:
print("Other error!")
}
}
}
Swift 3.x
Accepted answer didn't worked for me too.
This work for me!
let url = URL(string: "yourStringUrl")!
var urlRequest = URLRequest(url: url)
urlRequest.timeoutInterval = 5 // or what you want
And after:
Alamofire.request(urlRequest).response(completionHandler: { (response) in
/// code here
}
Swift 4
This my way and timeout feature is workable, meanwhile practices singleton for api class.
reference from here
struct AlamofireManager {
static let shared: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 5
let sessionManager = Alamofire.SessionManager(configuration: configuration, delegate: SessionDelegate(), serverTrustPolicyManager: nil)
return sessionManager
}()
}
class Auth {
static let api = Auth()
private init() {}
func headers() -> HTTPHeaders {
return [
"Accept": "XXX",
"Authorization": "XXX",
"Content-Type": "XXX"
]
}
func querySample() {
AlamofireManager.shared.request("api_post_url", method: .post, parameters: ["parametersKey": "value"], encoding: JSONEncoding.default, headers: headers())
.responseJSON(queue: DispatchQueue.global(), options: []) { (response) in
switch response.result {
case .success(let value):
// do your statement
case .failure(let error):
if error._code == NSURLErrorTimedOut {
// timeout error statement
} else {
// other error statement
}
}
})
}
func queryOtherSample() {
AlamofireManager.shared.request("api_get_url", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers())
.responseJSON(queue: DispatchQueue.global(), options: []) { (response) in
switch response.result {
case .success(let value):
// do your statement
case .failure(let error):
if error._code == NSURLErrorTimedOut {
// timeout error statement
} else {
// other error statement
}
}
})
}
}
For Swift 3.x / Swift 4.0 / Swift 5.0 users with Alamofire >= 5.0
Used request modifier to increase and decrease the timeout interval.
Alamofire's request creation methods offer the most common parameters for customization but sometimes those just aren't enough. The URLRequests created from the passed values can be modified by using a RequestModifier closure when creating requests. For example, to set the URLRequest's timeoutInterval to 120 seconds, modify the request in the closure.
var manager = Session.default
manager.request(urlString, method: method, parameters: dict, headers: headers, requestModifier: { $0.timeoutInterval = 120 }).validate().responseJSON { response in
OR
RequestModifiers also work with trailing closure syntax.
var manager = Session.default
manager.request("https://httpbin.org/get") { urlRequest in
urlRequest.timeoutInterval = 60
urlRequest.allowsConstrainedNetworkAccess = false
}
.response(...)
You can also check it here
Make extension of SessionManager and write a public static variable like this,
"requestTimeOutInterval" this is a public variable. it has time.
extension SessionManager {
public static let custom: SessionManager = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = requestTimeOutInterval
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
return SessionManager(configuration: configuration)
}()
}
Swift 5.0, Alamofire 5.4.2
The error code when time out always equal to NSURLErrorTimedOut, so
I try to retrieve Error object from AFError and upcast to NSError.
extension AFError {
var isTimeout: Bool {
if isSessionTaskError,
let error = underlyingError as NSError?,
error.code == NSURLErrorTimedOut //-1001
{
return true
}
return false
}
}
Invoke on response closure.
let request = URLRequest(url: URL(string: "https://httpbin.org/delay/10")!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 2)
AF.request(request).responseString(completionHandler: { response in
switch response.result {
case .success(_):
print("success")
case .failure(let error):
if error.isTimeout {
print("Timeout!")
}
}
})
In Alamofire 5.5 SessionManager has been renamed Session
Here is the documentation link
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%205.0%20Migration%20Guide.md#breaking-api-changes
Also the example of user
let manager = Alamofire.Session.default
manager.session.configuration.timeoutIntervalForRequest = 15
let headers: HTTPHeaders? = token == nil ? nil : [.authorization(bearerToken: token!),.accept("application/json")]
manager.request(path, method: method, parameters: parameter, headers: headers).responseJSON { (response) in
switch response.result {
case .success:
case .failure:
}
}