views:

28

answers:

3

i have a pretty basic iphone app, makes some web calls with:

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

In testing, if I stop the app, turn off wireless, and restart the app (not kill and restart, just bring to foreground), I continue to get errors on any network url attempts. But other apps (safari, etc) restore fine.

Is there something I'm missing in needing to "clear" the network state in my app if it goes down while the app is in the background?

+1  A: 

Try using asynchronous network requests instead. There could possibly be some sort of issue with suspending and resuming a blocked synchronous network thread during some error condition.

hotpaw2
+1  A: 

I don't know why you're seeing the behavior that your are, but I do have two suggestions.

First, unless you're already running on a background thread, avoid synchronous requests. Apple's URL Loading System Programming Guide has some sample code for asynchronous requests. The documentation for NSURLConnection also has links to lots of sample code under "Related sample code".

However, an even better options is to use ASIHTTPRequest. It's a very robust, flexible, and easy to use library for downloading data from the web. It's used in dozens, if not hundreds (if not thousands) of applications, and is very good at handling connection problems. Chances are good that it already handles the case you've described above.

Robot K
+1  A: 

I believe there are things that must be done to support your application running in the background, and actions that must be done to ensure it functions properly when brought to the foreground

https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Aaron Saunders
Here's a link that doesn't require a login. https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
Robot K