NSURLConnection timeout Problem

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

  1. Paresh-Nawale macrumors newbie

    Joined:
    Aug 15, 2009
    #1
    Hi,

    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.

    Thanks,
    Paresh
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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

    Joined:
    Aug 15, 2009
    #3
    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?

    Thanks,
    Paresh

     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    apparently 240 secs

    https://devforums.apple.com/thread/25282

    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.

    https://devforums.apple.com/message/37677
     
  5. Paresh-Nawale thread starter macrumors newbie

    Joined:
    Aug 15, 2009
    #5
    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?

    Thanks,
    Paresh

     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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