NSURLConnection timeout Problem

Discussion in 'iOS Programming' started by Paresh-Nawale, Aug 15, 2009.

  1. Paresh-Nawale macrumors newbie

    Aug 15, 2009

    In one of my iPhone application I am using NSURLConnection class to connect to the web server. Following code use to work fine on iPhone SDK 2.1 but when I upgraded my iPhone SDK to 3.0 its stopped working.

    After call to sendSynchronousRequest method application hangs for a minute in case of wrong url but if url is valid application is working fine.

    Here is the code
    NSMutableURLRequest *theRequest=[[NSMutableURLRequest alloc] init];
    /* Set Requests post data and method */
    [theRequest setURL:[NSURL URLWithString:urlAddress]];
    [theRequest setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
    [theRequest setTimeoutInterval:3];

    theRequest.HTTPMethod = @"POST";
    theRequest.HTTPBody = [command dataUsingEncoding:NSASCIIStringEncoding];
    [theRequest setValue: @"text/xml" forHTTPHeaderField:mad:"Content-Type"];
    /* create the connection with the request and start loading the data */
    NSURLResponse *response;
    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];

    Even though I am setting timeout interval to 3 seconds its not responding at all.

    Can anyone help with problem or suggest an alternative way to download data from url synchronously.

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Don't use NSURLConnection synchronously.

    Apparently apple sets a minimum timeout and you can't set a lower timeout.

    Using asynchronous connections is the right way to do this.
  3. Paresh-Nawale thread starter macrumors newbie

    Aug 15, 2009
    Thanks for your reply.
    Still I am not clear about following two points

    1) What is the minimum timeout set by Apple?
    2) And my code use to wok fine on iPhone SDK 2.1 when I upgraded it to 3.0 its stopped working why is that?


  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    apparently 240 secs


    Something changed.

    Really, it doesn't matter why it doesn't work the way you want anymore, and I don't know why it changed. The current behavior is certainly within the description of how it's supposed to work.

    You were doing it wrong and you should change to do it the right way. It's not hard.

  5. Paresh-Nawale thread starter macrumors newbie

    Aug 15, 2009
    Thanks a lot for your much needed help.
    I read the articles you posted in the last thread. Those were really helpful.

    I just wanted to know one more thing is there anyway to achieve synchronous way using asynchronous call?


  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Not really. You could launch a thread that launches an asynchronous load and wait for the result but that's the same as what the NSURLConnection synchronous call does. The point is that you shouldn't block the main thread with anything that might take a long time to complete.
  7. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Yeah, you seem kind of hung up on the synchronous approach. Is there a reason? Do you have some requirement that makes synchronous that much better of a solution than asynchronous?

Share This Page