Post not yet marked as solved
I was going through the documentation of dataTaskWithRequest:completionHandler: method of NSURLSession
https://developer.apple.com/documentation/foundation/nsurlsession/1407613-datataskwithrequest
The completion handler has the third param as NSError. But I could not find the domain name for these errors. Is it expected to be NSURLErrorDomain if the error is related to the network request? Or is it kCFErrorDomainCFNetwork. Is there any exhaustive list of error domain names that one can encounter for the above method?
Thanks!
Post not yet marked as solved
I wanted to implement a retry mechanism for a NSURLSessionDataTask. In android I am seeing that we can simply set a retry policy for the volley request as such
myRequest.setRetryPolicy(new DefaultRetryPolicy(
(int) TimeUnit.SECONDS.toMillis(200),
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Ref: https://afzaln.com/volley/com/android/volley/DefaultRetryPolicy.html
I tried finding something similar for NSURLSessionDataTask but havent been able to find it yet. Is there any iOS SDK support for this?
Or do we need to implement something like below pseudocode for retries with backoff?
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[self callServiceWithRetries:3 timeAtStart:[NSDate date] andTimeOut:200];
id callServiceWithRetries:(int)retries
timeAtStart:(NSDate)timeAtStart
andTimeOut:(int)timeout{
__weak __typeof(self) weakSelf = self;
NSURLRequest *request = //create request
NSDate *startDate = [NSDate date];
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDate *endDate = [NSDate date];
NSInterval timeIntervalSinceStart = [endDate timeIntervalSinceDate:timeAtStart];
NSInteger timeSinceStart = ((timeIntervalSinceStart % 1) * 1000);
if (error) {
if(retries > 0 && timeSinceStart < timeOut){
// add delay/backoff here before making the request. To calculate the delay we can
// use the current number of retries made. Something like exponential back off here
[weakSelf callServiceWithRetries:retries - 1 timeAtStart:timeAtStart andTimeout:timeout];
}
else{
//failure callback
}
return;
}
if(non 2xx error){
if(retries > 0 && timeSinceStart < timeOut){
// add delay/backoff here before making the request. To calculate the delay we can
// use the current number of retries made. Something like exponential back off here
[weakSelf callServiceWithRetries:retries - 1 timeAtStart:timeAtStart andTimeout:timeout];
}
else{
//failure callback
}
}
//success callback
}
}