Tenthrow
Tenthrow

Reputation: 51

NSURLConnection is not calling didFailWithError.

I am attempting to write a bit of code that checks the URL of a datasource, then populates an array with objects from that URL. It actually works well, but if there is a problem with the web connection or the address I want to populate the array with data from a bundled file. The issue I am having is that the connection didFailWithError method is never called. I tried passing a simple string but it does not call. I want the app to still function for people who are using ipod touch or are in airplane mode.

connection didReceiveResponse is working without issue.

This is what I'm working with.

- (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]); }

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

listConnectFail = YES; 
NSLog(@"Connection Failed, pulling from file"); }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { listConnectFail = NO; NSLog(@"Connection Succeeded, populating from API"); }

I know it is probably something stupid that I am not seeing, but I could use the help to see what I don't

Thanks in advance!

Upvotes: 0

Views: 518

Answers (1)

J_D
J_D

Reputation: 3586

How did you confirm that your delegate did not receive the message? Did you check the log?

Your code seems to assume that 'listConnectFail' will be set immediately after the NSURLConnection's init is done, which is not necessarily the case.

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

The NSURLConnection documentation states that 'The delegate will receive delegate messages as the load progresses.'

However, I am not sure about the airplane mode, maybe this particular error can be detected synchronously.

Upvotes: 0

Related Questions