Fix blocks not called when same URL requested for download several times simultanously (fix #137)

This commit is contained in:
Olivier Poitrey 2012-06-05 01:16:52 +02:00
parent b78238802d
commit 248fbc2148
2 changed files with 17 additions and 11 deletions

View File

@ -39,6 +39,7 @@ typedef enum
*/ */
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate> @interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
{ {
NSMutableArray *downloadInfo;
NSMutableArray *downloadDelegates; NSMutableArray *downloadDelegates;
NSMutableArray *downloaders; NSMutableArray *downloaders;
NSMutableArray *cacheDelegates; NSMutableArray *cacheDelegates;

View File

@ -28,6 +28,7 @@ static SDWebImageManager *instance;
{ {
if ((self = [super init])) if ((self = [super init]))
{ {
downloadInfo = [[NSMutableArray alloc] init];
downloadDelegates = [[NSMutableArray alloc] init]; downloadDelegates = [[NSMutableArray alloc] init];
downloaders = [[NSMutableArray alloc] init]; downloaders = [[NSMutableArray alloc] init];
cacheDelegates = [[NSMutableArray alloc] init]; cacheDelegates = [[NSMutableArray alloc] init];
@ -40,6 +41,7 @@ static SDWebImageManager *instance;
- (void)dealloc - (void)dealloc
{ {
SDWISafeRelease(downloadInfo);
SDWISafeRelease(downloadDelegates); SDWISafeRelease(downloadDelegates);
SDWISafeRelease(downloaders); SDWISafeRelease(downloaders);
SDWISafeRelease(cacheDelegates); SDWISafeRelease(cacheDelegates);
@ -193,6 +195,7 @@ static SDWebImageManager *instance;
{ {
SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]); SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]);
[downloadInfo removeObjectAtIndex:idx];
[downloadDelegates removeObjectAtIndex:idx]; [downloadDelegates removeObjectAtIndex:idx];
[downloaders removeObjectAtIndex:idx]; [downloaders removeObjectAtIndex:idx];
@ -291,7 +294,6 @@ static SDWebImageManager *instance;
else else
{ {
// Reuse shared downloader // Reuse shared downloader
downloader.userInfo = info; // TOFIX: here we overload previous userInfo
downloader.lowPriority = (options & SDWebImageLowPriority); downloader.lowPriority = (options & SDWebImageLowPriority);
} }
@ -301,6 +303,7 @@ static SDWebImageManager *instance;
downloader.progressive = YES; downloader.progressive = YES;
} }
[downloadInfo addObject:info];
[downloadDelegates addObject:delegate]; [downloadDelegates addObject:delegate];
[downloaders addObject:downloader]; [downloaders addObject:downloader];
} }
@ -326,7 +329,7 @@ static SDWebImageManager *instance;
} }
if ([delegate respondsToSelector:@selector(webImageManager:didProgressWithPartialImage:forURL:userInfo:)]) if ([delegate respondsToSelector:@selector(webImageManager:didProgressWithPartialImage:forURL:userInfo:)])
{ {
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"]; NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class]) if ([userInfo isKindOfClass:NSNull.class])
{ {
userInfo = nil; userInfo = nil;
@ -365,7 +368,7 @@ static SDWebImageManager *instance;
} }
if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:userInfo:)]) if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:userInfo:)])
{ {
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"]; NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class]) if ([userInfo isKindOfClass:NSNull.class])
{ {
userInfo = nil; userInfo = nil;
@ -373,9 +376,9 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, downloader.url, userInfo); objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, downloader.url, userInfo);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"success"]) if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"success"])
{ {
SuccessBlock success = [downloader.userInfo objectForKey:@"success"]; SuccessBlock success = [[downloadInfo objectAtIndex:uidx] objectForKey:@"success"];
success(image); success(image);
} }
#endif #endif
@ -392,7 +395,7 @@ static SDWebImageManager *instance;
} }
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)]) if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{ {
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"]; NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class]) if ([userInfo isKindOfClass:NSNull.class])
{ {
userInfo = nil; userInfo = nil;
@ -400,15 +403,16 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, nil, downloader.url, userInfo); objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, nil, downloader.url, userInfo);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"failure"]) if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"])
{ {
FailureBlock failure = [downloader.userInfo objectForKey:@"failure"]; FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"];
failure(nil); failure(nil);
} }
#endif #endif
} }
[downloaders removeObjectAtIndex:uidx]; [downloaders removeObjectAtIndex:uidx];
[downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx]; [downloadDelegates removeObjectAtIndex:uidx];
} }
} }
@ -459,7 +463,7 @@ static SDWebImageManager *instance;
} }
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)]) if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{ {
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"]; NSDictionary *userInfo = [[downloadInfo objectAtIndex:uidx] objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class]) if ([userInfo isKindOfClass:NSNull.class])
{ {
userInfo = nil; userInfo = nil;
@ -467,14 +471,15 @@ static SDWebImageManager *instance;
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, error, downloader.url, userInfo); objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, error, downloader.url, userInfo);
} }
#if NS_BLOCKS_AVAILABLE #if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"failure"]) if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"])
{ {
FailureBlock failure = [downloader.userInfo objectForKey:@"failure"]; FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"];
failure(error); failure(error);
} }
#endif #endif
[downloaders removeObjectAtIndex:uidx]; [downloaders removeObjectAtIndex:uidx];
[downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx]; [downloadDelegates removeObjectAtIndex:uidx];
} }
} }