iphone app crashing when url connection connection timed out is coming
I'm creating iphone app. In that, when the app starts, it connects to server and downloads few images and then proceeds with app. When app is downloading, it will show the initial splash screen. as long as my server is able to ping my iphone, its working well. but the trouble starts when my server is taking much time to respond for the NSURL request. The app is crashing with following error:
Mon May 14 13:56:34 unknown Springboard[24] <Warning>: com.xxxx.xxx failed to launch in time
I understood that when such issues开发者_运维百科 happen with application, iphone crashes the appliation. I would like to know how much max time iphone allows app to respond to such instances.
Is there any max value for that?
The timer is something like 20-30 seconds, but that's not important.
You are downloading data synchronously. Please change your program to download asynchronously, using NSURLConnection. Your app will seem much faster and won't run the risk of termination. You can also implement error handling for timeouts.
From the Readme.txt on Apple's Reachability example:
The Reachability sample demonstrates the asynchronous use of the SCNetworkReachability API. You can use the API synchronously, but do not issue a synchronous check by hostName on the main thread. If the device cannot reach a DNS server or is on a slow network, a synchronous call to the SCNetworkReachabilityGetFlags function can block for up to 30 seconds trying to resolve the hostName. If this happens on the main thread, the application watchdog will kill the application after 20 seconds of inactivity.
As Paul says, it is a very, very bad idea to do any sort of synchronous networking. You need to do this loading asynchronously on the iPhone.
If your request and response operation handled in main thread, UI and main thread will get blocked and it may take some time to receive response. If main thread blocked for particular time the WATCH DOG will quit your application.
The better solution is run your request in background thread or some other thread.
For example
if(!backgroundQueue)
backgroundQueue=[[NSOperationQueue alloc]init];
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:60];
[NSURLConnection sendAsynchronousRequest:request
queue:backgroundQueue
completionHandler:^(NSURLResponse *response,NSData *data,NSError *error) {
if (complete) {
// handle your logic here
}
}];
This operation handled in background thread
精彩评论