AFNetworking/AFNetworking — Issue #214: Images with 404 response getting cached
Author: tleyden
Created: 2012-02-23 00:38:25+00:00
State: open

I'm loading remote images via:

NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];

AFImageRequestOperation *imageRequestOperation =
[AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
cacheName:nil
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
UIImage *image) {

[self setImage:image];

}
failure:^(NSURLRequest *request,
NSHTTPURLResponse *response,
NSError *error) {

[self setImage:failedPlaceholder];

}];

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:imageRequestOperation];

And I noticed that when the server returned a 404 for an image URL, that 404 response was getting cached, so that even when I fixed the 404 response on the server the correct image wouldn't load.

I noticed that in AFURLConnectionOperation this method:

- (NSCachedURLResponse *)connection:(NSURLConnection *)__unused connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse;

does not check the response code of the [cachedResponse response] object to make sure its not an error response.

Would it be a good idea to add a check here to only return a non-nil return value if the response code is in the 200-299 range?