NSURLConnection sendSynchronousRequest - foreground background

I use sendSynchronousRequest to receive data from the server. I know that synchronous will wait for the data received for this request.

But the problem arises when the user mistakenly enters some non-existent url and tries to get an answer. In this case, if the user goes to the background and comes to the fore, he only shows a black screen. It shows only the status bar. Also, no background application is displayed. I have to click the Home button to exit my application.

In the simulator. After 1 minute, he shows me a message that “Request time” (No crash).

On the device, the application crashes for 1 minute.

Any suggestion. Any help. This is a really serious problem in my application.

Thanks.

+6
source share
9 answers

As Julien said, a watchdog kills your application. To answer some questions:

  • Why does this only happen on the simulator? Because when you debug the watchdog, you will leave your application alone, it may take some time.
  • Why does this only happen when the user enters the wrong URL? Due to the system timeout, the system will try to use 60 seconds if it cannot find the server.
  • why is the problem synchronous and asynchronous? No, the problem is in the thread, you can do the same operation in the background thread, just do not do this in the main thread, and the watchdog will leave you alone.
  • Why is the screen black when the application appears? Remember that you are making blocking material in the main thread, the thread that draws ...

I hope that's all. Let me know if I missed something.

+14
source

Why not set a timeout for your connection?

NSString *urlString = TEST_CONNECTION; NSError *error = nil; NSHTTPURLResponse *response = nil; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0]; NSData *conn = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

This should release the synchronous wait after a few seconds, which should solve your problem without leaving the asynchronous call (which is sometimes not the right solution)

I know this works correctly because I am checking if I am connected to a specific VPN (where reachability flags completely fail).

+8
source

you should take a look at this article: https://developer.apple.com/library/ios/#qa/qa1693/_index.html

iOs contains a watchdog timer , if your application is locked for a long time during an operation in the main thread, this one will be killed. (more information about Watchdog: http://en.wikipedia.org/wiki/Watchdog_timer )

So, if you want to download something, do not upload it to the main stream.

+3
source

Relate

 UIImage *image = [self.imgCache objectForKey:urlString]; if(!image){ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURLResponse *response = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; NSLog(@"%@",response); UIImage *img = [UIImage imageWithData:data]; // if(img) { dispatch_sync(dispatch_get_main_queue(), ^{ [self.imgCache setObject:img forKey:urlString]; completionBlock(img); }); } }); } else{ completionBlock(image); } 
+2
source

use the ASIHttpRequest class instead of NSURLConnection, its nothing but a wrapper around NSURLConnection and has very simple callbacks, you can also set the time to complete the request. Follow this link for more information http://allseeing-i.com/ASIHTTPRequest/

0
source

I think that you first need to check the user’s data, is it right or not, and only if it’s right, sends a request, otherwise offer the user "enter the correct data" ...

or

when your parsing of the data in response failed. You can also make ie FinishWithError protocol delegation method so that you come up with your latest user interface.

0
source

Try the following:

 #import "ASIHTTPRequest.h" //In a method [self performSelectorInBackground:@selector(DownLoadImageInBackground:) withObject:imgUrlArr]; -(void) DownLoadImageInBackground:(NSArray *)imgUrlArr1 { NSURL * url = [Image URL]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } -(void)requestFailed:(ASIHTTPRequest *)request { NSLog(@"URL Fail : %@",request.url); NSError *error = [request error]; // you can give here alert too.. } -(void)requestFinished:(ASIHTTPRequest *)request { NSData *responseData = [request responseData]; UIImage *imgInBackground = [[UIImage alloc] initWithData:responseData]; [imageView setImage: imgInBackground]; } 

This can help you: I also upload several images at once, so images that do not have the correct data show a black screen. To avoid this, try resizing the image.

0
source

You can check the availability of the URL before starting the request. Apple has Reachability Ways to do this. But its easier to use wrappers. For instance. ASIReachability .

0
source

I think the application crashes because you are not getting any data when the user enters the wrong URL and you use this "returned" nil NSData to do things.

I think this will fix your problem.

 NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if(data!=nil){ /// } else { NSLog(@"NO DATA"); } 
-1
source

Source: https://habr.com/ru/post/900582/


All Articles