NSURLConnection Delegate not working

1.2k views Asked by At

I have searched and tried a lot but in my application (which is using iOS 5 sdk) the NSURLConnection delegate methods gets called only when I initialise and start the connection in viewDidLoad method or on click event of any button.

But I want to call it after parsing my JSON file. And create a new connection for every record in JSON.

Can anyone tell me what is happening? Is something changed in ios sdk 5.

Here is my code:

-(void)getData:(NSString*)URL{
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];

shortURL = [NSString stringWithString:URL];


connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

//NSAssert(connection!=nil, @"no connection", nil);
[connection start];

}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response

{ // application specific stuff }

The getData function is written in a class inherited from NSObject. And it is called from

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method of UITableView.

Please help me with this.

1

There are 1 answers

0
Jason Harwig On

Assuming that the JSON parsing code is happening in a background thread, you have to create an NSRunLoop for that thread.

The NSURLConnection class is simply adding itself to the runloop, if none exists, no processing or delegate methods will get invoked.

connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[[NSRunLoop currentRunLoop] run];

Note: The run method will block