Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

NSURLConnection, delegate methods dont get called

estupefactikaestupefactika Posts: 65Registered Users
edited May 2009 in iPhone SDK Development
Hi, I've setup a class as a delegate for NSURLConnection, I
create the NSURLConnection, provide it a delegate (self) but the
delegate methods don't get called (didReceiveData and DidFinishLoading), any idea? Thanks
//In my method
for (int i=0;i<[feedArray count];i++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		imgUrl = [NSURL URLWithString:[[mainDelegate.feedArray objectAtIndex:i] objectForKey:@"img"]];

		AsyncImageView *async=[[AsyncImageView alloc] init];
		[async loadImageFromURL:imgUrl];
		[async release];
[pool release];			
}




//My class
@implementation AsyncImageView
@synthesize urlImg;
- (id) init
{
	
	if (self = [super init]) { 		
		appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
	}
	return self; 
}


- (void)loadImageFromURL:(NSURL*)url{
	
    if (connection!=nil) { [connection release]; }
    if (data!=nil) { [data release]; }
    NSURLRequest* request = [NSURLRequest requestWithURL:url 		 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

	connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
//I can see in logs some like:
//<NSURLConnection: 0x10d19d0, http://www.myurl.com/images/thumbnails/08_01.jpg>;


    //TODO error handling, what if connection is nil?
}


- (void)connection:(NSURLConnection *)theConnection	didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
		data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
	
	
    [connection release];
    connection=nil;
	UIImage *img=[UIImage imageWithData:data];
	.....
	
    [data release];
    data=nil;
 
}

- (void)dealloc {
    [connection cancel];
    [connection release];
    [data release];
    [super dealloc];
}

Post edited by estupefactika on

Replies

  • bgeerdesbgeerdes Posts: 99Registered Users
    edited May 2009
    You might want to implement didReceiveResponse, didReceiveAuthenticationChallenge and didFailWithError to see if you're getting anything there.
  • estupefactikaestupefactika Posts: 65Registered Users
    edited May 2009
    bgeerdes wrote: »
    You might want to implement didReceiveResponse, didReceiveAuthenticationChallenge and didFailWithError to see if you're getting anything there.

    Ive added:
    - (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error 
    { 
    	// release the connection, and the data object
    	[theConnection release]; 
    	// receivedData is declared as a method instance elsewhere 
    	[data release]; 
    	// inform the user 
    	NSLog(@"Connection failed! Error - %@ %@", 
    		  [error localizedDescription], 
    		  [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons {
    	NSLog(@"%@",respons);
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    	NSLog(@"%@",challenge);
    }
    

    They never aren't called.

    Im using this class twice. The first time it works fine, I have a table with an image in each cell, each one it inits the connection, I receive data and it finish loading the image in the cell.

    In the second time Im downloading the rest of images in background, is here where it dont works. I have a itineration "for" where I init the connection for each image. Once this method finish:
    - (void)loadImageFromURL:(NSURL*)url {
      
        if (connection!=nil) {NSLog(@"0");[connection release]; }
        if (data!=nil) {NSLog(@"1");[data release]; }
        NSURLRequest* request = [NSURLRequest requestWithURL:url 	 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    
    Inmediatelly the method dealloc is called, it is executed a couple of times:
    - (void)dealloc {
        [connection cancel];
        [connection release];
        [data release];
        [super dealloc];
    }
    

    and no delegates are called, nor didFailWithError, nor didReceiveResponse, nor didReceiveAuthenticationChallenge, nor didReceiveData.

    Im going to try to make other class to download the rest of images in background, maybe there is any conflict
  • bgeerdesbgeerdes Posts: 99Registered Users
    edited May 2009
    It sounds like the NSURLConnection isn't staying alive long enough to return you the image. Do you have logic to keep a connection from being overwritten by subsequent connections?
  • estupefactikaestupefactika Posts: 65Registered Users
    edited May 2009
    I think I've found the problem, where I was calling to my downloadImages method there was a nsautoreleasepool, I think by this my connection was being released. Now I call my method out and it works fine. Thanks
    - (void) doNewThreadStuff {
    
    	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    		
    	if ([mainDelegate.feedArray count] == 0) {
    		//Parser xml	
    		
    		ParserXML *parser=[[ParserXML alloc] init];
    		NSString *path=[[NSString alloc] initWithString:URL_XML];
    		[parser parseXMLFileAtURL:path];
                    //I was calling downloadImages here		
    		//[self downloadImages];
    		[parser release];
            }
    	
    	[pool release];
    	[self performSelectorOnMainThread:@selector(newThreadDone) withObject:nil waitUntilDone:NO];
    	
    }
    
    -(void) newThreadDone {
       [self downloadImages];
    }
    
    
    -(void) downloadImages {
    for (int i=0;i<[mainDelegate.feedArray count];i++) {
    		imgUrl = [NSURL URLWithString:[[mainDelegate.feedArray objectAtIndex:i] objectForKey:@"img"]];
    		AsyncImageView *async=[[AsyncImageView alloc] init];
    		[async loadImageFromURL:imgUrl];
    		[async release];
    }
    
    }
    
Sign In or Register to comment.