[英]iOS app crashing if data is inaccessible
I'm using the following to request data using NSJSONSerialization
. 我正在使用以下内容使用
NSJSONSerialization
请求数据。 The problem I'm having is that if the data is inaccessible (eg no network connection) the app crashes. 我遇到的问题是,如果无法访问数据(例如,没有网络连接),应用程序将崩溃。 How could I go about stopping the app from crashing if the network or server is down?
如果网络或服务器出现故障,如何停止应用程序崩溃?
I'm calling [self requestData];
我打电话给
[self requestData];
in the viewDidLoad:
method 在
viewDidLoad:
方法中
-(void)requestData {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"http://example.com/api/nodes"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *publicData = [NSJSONSerialization JSONObjectWithData:response
options:0
error:&jsonParsingError];
publicDataArray = [publicData objectForKey:@"data"];
for(publicDataDict in publicDataArray) {
NSLog(@"data output is %@",[publicDataDict objectForKey:@"title"]);
}
}
thanks for any help 谢谢你的帮助
Some thoughts: 一些想法:
Here the issue is: 这里的问题是:
You are calling a synchronous
request in the viewDidLoad
using sendSynchronousRequest
. 您正在使用
sendSynchronousRequest
在viewDidLoad
中调用synchronous
请求。 But the server is down, so you won't get the result, and it still expect any data to come. 但是服务器已关闭,因此您将无法获得结果,并且它仍然希望有任何数据来临。 But your app won't load untill that request finishes.
但是,直到该请求完成,您的应用才会加载。 Due to this springboards application-watchdog will terminate your app.
由于这个跳板,应用程序看门狗将终止您的应用程序。
What is Watch dog ? 什么是看门狗 ?
watchdog — In order to keep the user interface responsive, iOS includes a watchdog mechanism.
看门狗—为了保持用户界面的响应速度,iOS包含了看门狗机制。 If your application fails to respond to certain user interface events (launch, suspend, resume, terminate) in time, the watchdog will kill your application and generate a watchdog timeout crash report.
如果您的应用程序未能及时响应某些用户界面事件(启动,挂起,恢复,终止),则监视程序将终止您的应用程序并生成监视程序超时崩溃报告。 The amount of time the watchdog gives you is not formally documented, but it's always less than a network timeout.
看门狗给您的时间没有正式记录,但始终少于网络超时。
Please check this Technical question on Apple site. 请在Apple网站上检查此技术问题 。
Why don't you check if [NSURLConnection sendSynchronousRequest:]
got any error? 为什么不检查
[NSURLConnection sendSynchronousRequest:]
有任何错误?
NSError *requestError = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&requestError];
if (requestError)
{
NSLog(@"sync. request failed with error: %@", requestError);
}
else
{
// handle data
}
And you really should check if NSJSONSerialization
had an error too: 而且您确实应该检查
NSJSONSerialization
是否也有错误:
NSError *jsonParsingError = nil;
NSDictionary *publicData = [NSJSONSerialization JSONObjectWithData:response
options:0
error:&jsonParsingError];
if (jsonParsingError)
{
NSLog(@"JSON parsing failed with error: %@", jsonParsingError);
}
else
{
// do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.