With Alamofire is possible to get an event when request is sent whatever response is received or not? Just like with this URLSession method:
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;
Edit: My code is send a JSON to the server:
Alamofire.request("http://...", method: HTTPMethod.post, parameters: params, encoding: JSONEncoding.default,
headers: [...]).responseJSON {
response in
if response.result.isSuccess {
print("result is Success")
} else {
print("result is Failure")
}
}
I want to handle what if the network is disconnected and I'd like to know that response is arrived, or not.
Thank you in advance for any help you can provide.
Response Validation
By default, Alamofire treats any completed request to be successful, regardless of the content of the response. Calling validate
before a response handler causes an error to be generated if the response had an unacceptable status code or MIME type.
Manual Validation
Alamofire.request("https://httpbin.org/get")
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseData { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
Automatic Validation
Automatically validates status code within 200..<300
range, and that the Content-Type
header of the response matches the Accept
header of the request, if one is provided.
Alamofire.request("https://httpbin.org/get").validate().responseJSON { response in
switch response.result {
case .success:
print("Validation Successful")
case .failure(let error):
print(error)
}
}
Statistical Metrics
Timeline
Alamofire collects timings throughout the lifecycle of a Request
and creates a Timeline
object exposed as a property on all response types.
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.timeline)
}
The above reports the following Timeline
info:
Latency
: 0.428 secondsRequest Duration
: 0.428 secondsSerialization Duration
: 0.001 secondsTotal Duration
: 0.429 secondsTaken from Alamofire Usage. U can have e deeper look.