57

I am trying to create a simple NSURLConnection to communicate with a server using a GET request. Connection works well, but delegates methods of NSURLConnection are never called..

Here is what am doing:

NSString *post = [NSString stringWithFormat:@"key1=%@&key2=%@&key3=%f&key4=%@", val1, val4, val3, val4];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease] ;

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.domain.com/demo/name/file.php?%@", post]]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

Have implemented the following delegate methods, but none of them is called..

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"did fail");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"did receive response ");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"did finish loading");
    [connection release];
}

Am I missing something?

visakh7
  • 26,380
  • 8
  • 55
  • 69
Manish Ahuja
  • 4,509
  • 3
  • 28
  • 35

4 Answers4

136

Try running the operation on main thread:

NSURLConnection * connection = [[NSURLConnection alloc] 
                                initWithRequest:request
                                       delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];
Art
  • 23,747
  • 29
  • 89
  • 101
  • 5
    what a stupid API, even when calling it on the main thread, I still need to do this. – yano Mar 28 '14 at 02:49
  • 3
    Explain on why you should run it in the main thread : https://github.com/rs/SDWebImage/issues/213#issuecomment-10102989 – Sisyphus Jan 23 '16 at 12:01
22

Are you calling this on a background thread? If you are performing this on a background thread, the thread is probably exiting before the delegates can be called.

visakh7
  • 26,380
  • 8
  • 55
  • 69
  • i tried to NSLog(@"Is%@ main thread", ([NSThread isMainThread] ? @"" : @" NOT") just before I create the connection, and it returns "is Main thread" – Manish Ahuja Apr 26 '11 at 07:23
  • for the connection to work correctly the calling thread’s run loop must be operating in the default run loop mode. Can you check this too – visakh7 Apr 26 '11 at 07:29
  • 3
    I found out what the problem is.. and I have got it working too. But i would still like some clarification on this.. Actually I created the connection inside Static method of a class, and the delegate functions were never called.. And when I created the connection inside an instance method, everything works just fine.. delgate functions are now getting called... So, now the question is whats the difference when the connection is created inside a static method or it is created inside a instance method? – Manish Ahuja Apr 26 '11 at 07:56
1

Try to check length for the received response it should not getting 0 byte of data.

sanjeev sharma
  • 313
  • 2
  • 9
1

Apart from checking if the request is called from the main thread, you can check if you give back execution time to the system (if you exit "main"). I had some test code that would stay in a loop until the delegate was called : it would never be called, because the system needs to do stuff in order for the delegate to be called, in the main thread.