Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Paresh-Nawale

macrumors newbie
Original poster
Aug 15, 2009
4
0
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
 
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.
 
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

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.
 
1) What is the minimum timeout set by Apple?
apparently 240 secs

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

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?
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
 
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

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
 
I just wanted to know one more thing is there anyway to achieve synchronous way using asynchronous call?

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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.