Search code examples
iosobjective-casynchronousnsurlconnectiondelay

Delay the http retry request task in iOS


I am retrying to send a HTTP async request in the event of a 4XX error. I am doing this in the NSURLConnection delegate methods:

      - (void)connectionDidFinishLoading:(NSURLConnection *)connection
        {

         if(httpResponseStatusCode == 404) 
         { 
             [MyModel doGet:[connection currentRequest].URL delegate:self timeout:HTTP_TIMEOUT ]; 
         }    
        }

But this request is being triggered too early (before the server has a response for me). How should I add a delay such that the request is triggered after a delay of 2 sec?

I tried using performSelector with delay but I get a EXC_BAD_ACCESS when executed.

Thanks in advance!


Solution

  • Try dispatch_after to delay the request call. Here's the snippet

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
            [MyModel doGet:[connection currentRequest].URL delegate:self timeout:HTTP_TIMEOUT ]; 
        });
    

    Thanks you.