enormego / EGOImageLoading

What if images on the iPhone were as easy as HTML?
http://developers.enormego.com
801 stars 220 forks source link

EGOImaveView doesn't show its image when the "setImageURL:" method is called after a successful EGOHTTPRequest asynchronous request #4

Closed mejibyte closed 13 years ago

mejibyte commented 13 years ago

Hi guys,

Suppose you have something like this, using the (great) EGOHTTPRequest library:

EGOHTTPRequest * request = [[EGOHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDidFinish:)];
[request startAsynchronous];

If you then call the "setImageURL:" message of an EGOImageView inside the "requestDidFinish:" method (that gets called when the HTTP request is finished) the image will never be loaded.

Here's a minimal application that fires the bug. Just run these steps to download the app: git clone git://github.com/andmej/EGOBug.git cd EGOBug git checkout 8ad29fe cat README

Then run it in the iPhone simulator and you will see what I'm talking about.

mejibyte commented 13 years ago

Fixed!

What you have to do to overcome this is run the "setImageURL:" on the main thread, and not directly on the requestDidFinish: method. So basically just write:

-(void) requestDidFinish:(EGOHTTPRequest *)aRequest {
    NSLog(@"Request to Google finished with code %d. Now loading the image...", aRequest.responseStatusCode);
    [buggyImageView performSelectorOnMainThread:@selector(setImageURL:) withObject:[NSURL URLWithString:@"http://static.gowalla.com/kinds/1739-3700293ea3dcb1478289217052a588ab-100.png"] waitUntilDone:NO];
    NSLog(@"And it will load.");
}

instead of

-(void) requestDidFinish:(EGOHTTPRequest *)aRequest {
NSLog(@"Request to Google finished with code %d. Now loading the image...", aRequest.responseStatusCode);
[buggyImageView setImageURL:[NSURL URLWithString:@"http://static.gowalla.com/kinds/1739-3700293ea3dcb1478289217052a588ab-100.png"]];
NSLog(@"And it will never load.");
}