我正在尝试编写一些代码来检查数据源的URL,然后用该URL中的对象填充一个数组。它实际上工作得很好,但如果web连接或地址有问题,我想用捆绑文件中的数据填充数组。我遇到的问题是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*)连接错误:(NSError*)错误{
listConnectFail = YES;
NSLog(@"Connection Failed, pulling from file"); }我知道这可能是我没有看到的愚蠢的事情,但我可以利用帮助来看到我没有看到的东西
提前感谢!
发布于 2012-04-11 04:03:57
您是如何确认您的代表没有收到消息的?你查过日志了吗?
您的代码似乎假定'listConnectFail‘将在NSURLConnection的初始化完成后立即设置,但事实并非如此。
[[NSURLConnection alloc] initWithRequest:listURLRequest delegate:self];
if (!listConnectFail){...}NSURLConnection文档声明“在加载过程中,委派将收到委派消息”。
但是,我不确定飞行模式,也许这个特殊的错误可以同步检测到。
https://stackoverflow.com/questions/10095512
复制相似问题