"-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data" not called

11,875

Solution 1

I suspect you are having a memory management issue. I could be mistaken on this, but I believe that even:

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

won't work, because connection will be released at the end of the containing method, when connection goes out of scope. Make sure NSURLConnection *connection and NSMutableData *data are declared as member variables where ever you are doing this, and that you alloc and init them appropriately. My code usually looks like:

    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                          timeoutInterval:30.0];
    // cancel any old connection
    if(connection) {
        [connection cancel];
        [connection release];
    }
    // create new connection and begin loading data
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(connection) {
        // if the connection was created correctly, release old data (if any), and alloc new
        [data release];
        data = [[NSMutableData data] retain];
    }

Also, release the connection and data in dealloc. For good measure, release and set them to nil at the very end of didFailWithError and didFinishLoading:

[connection release];
connection = nil;
[data release];
data = nil;

Good luck; I've done this a million times, let me know if you cannot get it working.

Solution 2

You don't happen to be calling the NSConnection in a thread do you? If you are then what's happening is that the thread is terminating before NSConnection and its delegates have finished so it'll just bomb out without an error.

A workaround for this is in this thread

Share:
11,875
SocialCircus
Author by

SocialCircus

Updated on June 04, 2022

Comments

  • SocialCircus
    SocialCircus almost 2 years

    Have a look to this code snippet:-

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {               
        [webData setLength: 0];           
    }
    
    
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    
    {
        NSLog(@"Recieving Data...");
        [webData appendData:data];
    
    }
    
    
    
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"ERROR with theConenction");
        [connection release];
        [webData release];
    }
    
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"DONE. Received Bytes: %d", [webData length]);
        NSLog(theXML);
    
    
    }   
    

    I am calling a SOAP web service.There are no errors or warnings displayed in my code. When I hit the web service through safari it works fine. But the problem arises when I try hit it through my codes. Everything works fine but the connection:didRecieveData does not gets called. Thus, I get no data in the webData variable. This webData is a NSMutableData object. The problem seems to be silly but any one with any answers ....

    Thank You All.

  • Admin
    Admin almost 14 years
    Thanks Mickey, But I still have the same issue, its not working. I tested the URL in my browser, its loading there. Can you please help me please?
  • VdesmedT
    VdesmedT over 13 years
    Nothing will be release just because a variable pointing to it got out of scoop ! The connection object don't need to be retained and should be release in the delegate's callback after it has finnish its job.