Skip to content

Commit

Permalink
Merge pull request #92 from andybee/master
Browse files Browse the repository at this point in the history
Fix for SDWebImageManger block issue (84)
  • Loading branch information
Olivier Poitrey committed Apr 13, 2012
2 parents e63e925 + 5cacab1 commit f89508d
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions SDWebImageManager.m
Expand Up @@ -14,22 +14,12 @@
#if NS_BLOCKS_AVAILABLE
typedef void(^SuccessBlock)(UIImage *image);
typedef void(^FailureBlock)(NSError *error);

@interface SDWebImageManager ()
@property (nonatomic, copy) SuccessBlock successBlock;
@property (nonatomic, copy) FailureBlock failureBlock;
@end
#endif

static SDWebImageManager *instance;

@implementation SDWebImageManager

#if NS_BLOCKS_AVAILABLE
@synthesize successBlock;
@synthesize failureBlock;
#endif

- (id)init
{
if ((self = [super init]))
Expand Down Expand Up @@ -122,9 +112,29 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)del
#if NS_BLOCKS_AVAILABLE
- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure
{
self.successBlock = success;
self.failureBlock = failure;
[self downloadWithURL:url delegate:delegate options:options];
// repeated logic from above due to requirement for backwards compatability for iOS versions without blocks

// Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't
// throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString.
if ([url isKindOfClass:NSString.class])
{
url = [NSURL URLWithString:(NSString *)url];
}

if (!url || !delegate || (!(options & SDWebImageRetryFailed) && [failedURLs containsObject:url]))
{
return;
}

// Check the on-disk cache async so we don't block the main thread
[cacheDelegates addObject:delegate];
[cacheURLs addObject:url];
SuccessBlock successCopy = [success copy];
FailureBlock failureCopy = [failure copy];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithInt:options], @"options", successCopy, @"success", failureCopy, @"failure", nil];
SDWIRelease(successCopy);
SDWIRelease(failureCopy);
[[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info];
}
#endif

Expand Down Expand Up @@ -192,9 +202,10 @@ - (void)imageCache:(SDImageCache *)imageCache didFindImage:(UIImage *)image forK
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, url);
}
#if NS_BLOCKS_AVAILABLE
if (self.successBlock)
if ([info objectForKey:@"success"])
{
self.successBlock(image);
SuccessBlock success = [info objectForKey:@"success"];
success(image);
}
#endif

Expand Down Expand Up @@ -266,9 +277,10 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, downloader.url);
}
#if NS_BLOCKS_AVAILABLE
if (self.successBlock)
if ([downloader.userInfo objectForKey:@"success"])
{
self.successBlock(image);
SuccessBlock success = [downloader.userInfo objectForKey:@"success"];
success(image);
}
#endif
}
Expand All @@ -283,9 +295,10 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, nil, downloader.url);
}
#if NS_BLOCKS_AVAILABLE
if (self.failureBlock)
if ([downloader.userInfo objectForKey:@"failure"])
{
self.failureBlock(nil);
FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
failure(nil);
}
#endif
}
Expand Down Expand Up @@ -340,9 +353,10 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSE
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, error, downloader.url);
}
#if NS_BLOCKS_AVAILABLE
if (self.failureBlock)
if ([downloader.userInfo objectForKey:@"failure"])
{
self.failureBlock(error);
FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
failure(error);
}
#endif

Expand Down

0 comments on commit f89508d

Please sign in to comment.