我正在使用sendSynchronousRequest从服务器获取数据。我知道同步会等到收到该请求的数据。
但是当用户错误地输入一些不存在的url而不是尝试获得响应时,问题就出现了。在这种情况下,如果用户进入后台并进入前台,则仅显示黑屏。它只显示状态栏。它也没有显示任何背景应用。我必须按Home键才能退出我的应用程序。
在模拟器上,1分钟后,它会显示“请求超时”的消息(没有崩溃)。
在设备上,1分钟内应用程序崩溃。
任何建议。任何帮助。这在我的应用中确实是一个严重的问题。
感谢。
答案 0 :(得分:14)
就像朱利安说的那样,看门狗正在杀死你的应用程序。回答一些问题:
希望这就是全部。如果我错过了什么,请告诉我。
答案 1 :(得分:8)
为什么不为连接设置超时?
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];
这应该会在几秒钟之后释放同步等待,这可以解决您的问题,而无需进行异步调用(有时候这不是正确的解决方案)
我知道这是正常的,因为这是我检查我是否连接到某个VPN(可达性标志完全失败)的方式。
答案 2 :(得分:3)
你应该看一下这篇文章:https://developer.apple.com/library/ios/#qa/qa1693/_index.html
iOs包含监视程序,如果您的应用程序在主线程上的操作被阻塞了很长时间,那么这个应用程序将被终止。 (有关Watchdog的更多详细信息:http://en.wikipedia.org/wiki/Watchdog_timer)
因此,如果您想下载某些内容,请不要在主线程上下载。
答案 3 :(得分:2)
<强> 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);
}
答案 4 :(得分:0)
使用ASIHttpRequest类而不是NSURLConnection,它只是NSURLConnection的包装并且具有非常简单的回调,您还可以设置时间来完成请求。请仔细阅读此链接以获取更多信息http://allseeing-i.com/ASIHTTPRequest/
答案 5 :(得分:0)
我认为您首先必须测试用户数据是否正确,并且只有在正确的情况下,才会发送请求,否则提示用户“请输入正确的数据”......
或
当您在响应中解析数据失败时。您还可以创建协议委托方法,即FinishWithError,以便您提供最后一个UI。
答案 6 :(得分:0)
试试这个:
#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];
}
这可能会对您有所帮助:我一次也会加载一些图像,因此没有正确数据的图像会显示黑屏。要避免这种情况,请尝试调整imageview的大小。
答案 7 :(得分:0)
您可以在开始请求之前检查URL的可访问性。
Apple有Reachability
种方法。但它更容易使用包装。例如。 ASIReachability
。
答案 8 :(得分:-1)
我认为应用程序崩溃是因为当用户输入错误的URL
时您没有获得任何数据,并且您正在使用此'返回'nil
NSData
来执行操作。
我认为这会解决您的问题
NSData *data=[NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if(data!=nil){
///
} else {
NSLog(@"NO DATA");
}