0
votes

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?

If there is no support does the below pseudocode look acceptable 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
    }

}