error-handling - NSURLConnection没有调用didFailWithError。

标签 error-handling timeout nsurlconnection nsurlconnectiondelegate

我试图编写一些代码来检查数据源的URL,然后使用该URL中的对象填充数组。它实际上运行良好,但是如果Web连接或地址有问题,我想用 bundle 文件中的数据填充数组。我遇到的问题是从未调用connection didFailWithError方法。我尝试传递一个简单的字符串,但未调用。我希望该应用程序仍能对使用ipod touch或处于飞行模式的人起作用。
connection didReceiveResponse运行正常。

这就是我正在使用的。

- (void)loadListData{
NSLog(@"Loading data from sources");

NSURLRequest *listURLRequest = [NSURLRequest requestWithURL:integerPhoneListURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1.0];
[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];

  if (!listConnectFail){
        phoneListJSON =[NSData dataWithContentsOfURL:integerPhoneListURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];


    } else {
        //This will tell us if there is an error loading the file
        NSLog(@"File not found on web init from file");
        phoneListJSON =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contactlist" ofType:@"json"]];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:phoneListJSON waitUntilDone:YES];
    }



//Initialize the filtered list with array of customer objects. Based on original data
filteredList = [[NSMutableArray alloc] init];

for (NSDictionary *dict in phoneListOriginal) {
    contact *single = [[contact alloc] init];
    single.fName = [dict objectForKey:@"fName"];
    single.lName = [dict objectForKey:@"lName"];
    single.extension = [dict objectForKey:@"extension"];
    single.title = [dict objectForKey:@"title"];
    single.department = [dict objectForKey:@"department"];
    single.cellNumber = [dict objectForKey:@"cellNumber"];
    //NSLog(@"%@", single.lName);
    [filteredList addObject:single];
}


NSLog(@"Array filteredLIst contains %d records",[filteredList count]); }

-(无效)连接:(NSURLConnection *)连接didFailWithError:(NSError *)错误{
listConnectFail = YES; 
NSLog(@"Connection Failed, pulling from file"); }

-(void)连接:(NSURLConnection *)连接didReceiveResponse:(NSURLResponse *)响应{
listConnectFail =否;
NSLog(@“连接成功,从API填充”);
}

我知道我可能没有看到这很愚蠢,但是我可以使用帮助来查看我没有看到的东西

提前致谢!

最佳答案

您如何确认您的代表未收到该消息?您检查日志了吗?

您的代码似乎假设NSURLConnection的init完成后会立即设置“listConnectFail”,而事实并非一定如此。

[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];
if (!listConnectFail){...}

NSURLConnection文档指出,“代理将在加载过程中接收代理消息”。

但是,我不确定飞行模式,也许可以同步检测到此特定错误。

关于error-handling - NSURLConnection没有调用didFailWithError。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10095512/

相关文章:

Javascript For/in 循环不起作用 - 对象属性未定义错误

swift - 在 Swift 中将可选绑定(bind)转换为错误处理的过程是怎样的?

C# "Sometimes"客户端断开连接后,套接字监听器出现错误

java - 移动设备上连接的适当超时是多少?

android - 如何检查OkHttp3请求是否超时?

iphone - iOS 5 NSURLConnection - 通过 UI 反馈建立多个连接

asp.net-core - 401初始错误未调用ASP.NET核心应用程序.UseStatusCodePagesWithReExecute

javascript - 在 JavaScript 中对空闲状态执行操作

iphone - NSoperation如何使用它来等待事件?

iphone - UIImage 从服务器逐步显示