CodeGeek123
CodeGeek123

Reputation: 4501

NSURLConnection Delegate methods are not getting called

I have had this problem and i went through so many posts here on stack overflow and everywhere else and couldnt find a solution. Im running it on the main thread as well. Code as follows.

@interface JsonViewController : UIViewController <UIActionSheetDelegate,UIWebViewDelegate,NSURLConnectionDelegate>
{
  ....
}
@implementation JsonViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
    rssFeedDetailViewConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"PRINTING RSS FEED %@", rssFeedDetailViewConnection);
[rssFeedDetailViewConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                                       forMode:NSDefaultRunLoopMode];
    [rssFeedDetailViewConnection start];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Hello");
responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
     NSLog(@"Hello");
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
     NSLog(@"Hello");
    [responseData release];
    [connection release];
// Show error message
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 NSLog(@"Hello");
    // Use responseData
    [responseData release];
    [connection release];
 }

Any help would be greatly appreciated. Ive been stuck with this for two days now..

Upvotes: 0

Views: 1548

Answers (2)

CodeGeek123
CodeGeek123

Reputation: 4501

Have a boolean called finished and add this code where ever you have written your NSURLConnection Code.

 while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

and in your connectionDidFinishLoading just add this

 finished = TRUE; 

and that works. Its not the best solution i understand but it works.

Upvotes: 0

fguchelaar
fguchelaar

Reputation: 4909

You don't need these statements:

[rssFeedDetailViewConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]  forMode:NSDefaultRunLoopMode];
[rssFeedDetailViewConnection start];

since you're using initWithRequest:delegate:. This call already starts loading the data.

What happens if you remove these particular statements?

More info on the NSURLConnection.

Upvotes: 1

Related Questions