From 1fc909e9db6850f2aed4b03349c978e9765cbc7f Mon Sep 17 00:00:00 2001 From: Philippe Converset Date: Wed, 31 Jul 2013 11:25:55 +0200 Subject: [PATCH] Allow nested call to setImageWithURLRequest Calling [UIImageView setImageWithURL:] inside [UIImageView setImageWithURLRequest:placeholderImage:success:failure] completion block could NOT work. This was due to internal af_imageRequestOperation set to nil AFTER a completion block is called. With this fix, it is now set to nil BEFORE. --- AFNetworking/UIImageView+AFNetworking.m | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/AFNetworking/UIImageView+AFNetworking.m b/AFNetworking/UIImageView+AFNetworking.m index b577cf1..f0f8274 100644 --- a/AFNetworking/UIImageView+AFNetworking.m +++ b/AFNetworking/UIImageView+AFNetworking.m @@ -101,13 +101,13 @@ static char kAFImageRequestOperationObjectKey; UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest]; if (cachedImage) { + self.af_imageRequestOperation = nil; + if (success) { success(nil, nil, cachedImage); } else { self.image = cachedImage; } - - self.af_imageRequestOperation = nil; } else { if (placeholderImage) { self.image = placeholderImage; @@ -116,27 +116,27 @@ static char kAFImageRequestOperationObjectKey; AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest]; [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if ([urlRequest isEqual:[self.af_imageRequestOperation request]]) { + if (self.af_imageRequestOperation == operation) { + self.af_imageRequestOperation = nil; + } + if (success) { success(operation.request, operation.response, responseObject); } else if (responseObject) { self.image = responseObject; } - - if (self.af_imageRequestOperation == operation) { - self.af_imageRequestOperation = nil; - } } [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if ([urlRequest isEqual:[self.af_imageRequestOperation request]]) { - if (failure) { - failure(operation.request, operation.response, error); - } - if (self.af_imageRequestOperation == operation) { self.af_imageRequestOperation = nil; } + + if (failure) { + failure(operation.request, operation.response, error); + } } }];