Permalink
Browse files

Fix blocks not called when same URL requested for download several ti…

…mes simultanously (fix #137)
  • Loading branch information...
1 parent b782388 commit 248fbc2148fe1ea01c128a45fd05f354f61c0944 @rs committed Jun 4, 2012
Showing with 17 additions and 11 deletions.
  1. +1 −0 SDWebImage/SDWebImageManager.h
  2. +16 −11 SDWebImage/SDWebImageManager.m
@@ -39,6 +39,7 @@ typedef enum
*/
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
{
+ NSMutableArray *downloadInfo;
NSMutableArray *downloadDelegates;
NSMutableArray *downloaders;
NSMutableArray *cacheDelegates;
@@ -28,6 +28,7 @@ - (id)init
{
if ((self = [super init]))
{
+ downloadInfo = [[NSMutableArray alloc] init];
downloadDelegates = [[NSMutableArray alloc] init];
downloaders = [[NSMutableArray alloc] init];
cacheDelegates = [[NSMutableArray alloc] init];
@@ -40,6 +41,7 @@ - (id)init
- (void)dealloc
{
+ SDWISafeRelease(downloadInfo);
SDWISafeRelease(downloadDelegates);
SDWISafeRelease(downloaders);
SDWISafeRelease(cacheDelegates);
@@ -193,6 +195,7 @@ - (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate
{
SDWebImageDownloader *downloader = SDWIReturnRetained([downloaders objectAtIndex:idx]);
+ [downloadInfo removeObjectAtIndex:idx];
[downloadDelegates removeObjectAtIndex:idx];
[downloaders removeObjectAtIndex:idx];
@@ -291,7 +294,6 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
else
{
// Reuse shared downloader
- downloader.userInfo = info; // TOFIX: here we overload previous userInfo
downloader.lowPriority = (options & SDWebImageLowPriority);
}
@@ -301,6 +303,7 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
downloader.progressive = YES;
}
+ [downloadInfo addObject:info];
[downloadDelegates addObject:delegate];
[downloaders addObject:downloader];
}
@@ -326,7 +329,7 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didUpdatePartialImage
}
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])
{
userInfo = nil;
@@ -365,17 +368,17 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
}
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])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, downloader.url, userInfo);
}
#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);
}
#endif
@@ -392,23 +395,24 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
}
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])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, nil, downloader.url, userInfo);
}
#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);
}
#endif
}
[downloaders removeObjectAtIndex:uidx];
+ [downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx];
}
}
@@ -459,22 +463,23 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSE
}
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])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, error, downloader.url, userInfo);
}
#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);
}
#endif
[downloaders removeObjectAtIndex:uidx];
+ [downloadInfo removeObjectAtIndex:uidx];
[downloadDelegates removeObjectAtIndex:uidx];
}
}

0 comments on commit 248fbc2

Please sign in to comment.