Merge pull request #92 from andybee/master

Fix for SDWebImageManger block issue (84)
This commit is contained in:
Olivier Poitrey 2012-04-13 01:45:31 -07:00
commit f89508d6f7
1 changed files with 35 additions and 21 deletions

View File

@ -14,22 +14,12 @@
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
typedef void(^SuccessBlock)(UIImage *image); typedef void(^SuccessBlock)(UIImage *image);
typedef void(^FailureBlock)(NSError *error); typedef void(^FailureBlock)(NSError *error);
@interface SDWebImageManager ()
@property (nonatomic, copy) SuccessBlock successBlock;
@property (nonatomic, copy) FailureBlock failureBlock;
@end
#endif #endif
static SDWebImageManager *instance; static SDWebImageManager *instance;
@implementation SDWebImageManager @implementation SDWebImageManager
#if NS_BLOCKS_AVAILABLE
@synthesize successBlock;
@synthesize failureBlock;
#endif
- (id)init - (id)init
{ {
if ((self = [super init])) if ((self = [super init]))
@ -122,9 +112,29 @@ static SDWebImageManager *instance;
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure
{ {
self.successBlock = success; // repeated logic from above due to requirement for backwards compatability for iOS versions without blocks
self.failureBlock = failure;
[self downloadWithURL:url delegate:delegate options:options]; // 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 #endif
@ -192,9 +202,10 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, url); objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, url);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if (self.successBlock) if ([info objectForKey:@"success"])
{ {
self.successBlock(image); SuccessBlock success = [info objectForKey:@"success"];
success(image);
} }
#endif #endif
@ -266,9 +277,10 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, downloader.url); objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, downloader.url);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if (self.successBlock) if ([downloader.userInfo objectForKey:@"success"])
{ {
self.successBlock(image); SuccessBlock success = [downloader.userInfo objectForKey:@"success"];
success(image);
} }
#endif #endif
} }
@ -283,9 +295,10 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, nil, downloader.url); objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, nil, downloader.url);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if (self.failureBlock) if ([downloader.userInfo objectForKey:@"failure"])
{ {
self.failureBlock(nil); FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
failure(nil);
} }
#endif #endif
} }
@ -340,9 +353,10 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, error, downloader.url); objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, error, downloader.url);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if (self.failureBlock) if ([downloader.userInfo objectForKey:@"failure"])
{ {
self.failureBlock(error); FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
failure(error);
} }
#endif #endif