Merge pull request #1046 from hoppenichu/fix_downloader

Fix SDWebImageDownloader threading issue
This commit is contained in:
Olivier Poitrey 2015-02-24 21:53:57 -08:00
commit 0f157d4332
2 changed files with 15 additions and 21 deletions

View File

@ -135,7 +135,10 @@ static NSString *const kCompletedCallbackKey = @"completed";
progress:^(NSInteger receivedSize, NSInteger expectedSize) { progress:^(NSInteger receivedSize, NSInteger expectedSize) {
SDWebImageDownloader *sself = wself; SDWebImageDownloader *sself = wself;
if (!sself) return; if (!sself) return;
NSArray *callbacksForURL = [sself callbacksForURL:url]; __block NSArray *callbacksForURL;
dispatch_sync(sself.barrierQueue, ^{
callbacksForURL = sself.URLCallbacks[url];
});
for (NSDictionary *callbacks in callbacksForURL) { for (NSDictionary *callbacks in callbacksForURL) {
SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey]; SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
if (callback) callback(receivedSize, expectedSize); if (callback) callback(receivedSize, expectedSize);
@ -144,10 +147,13 @@ static NSString *const kCompletedCallbackKey = @"completed";
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
SDWebImageDownloader *sself = wself; SDWebImageDownloader *sself = wself;
if (!sself) return; if (!sself) return;
NSArray *callbacksForURL = [sself callbacksForURL:url]; __block NSArray *callbacksForURL;
if (finished) { dispatch_barrier_sync(sself.barrierQueue, ^{
[sself removeCallbacksForURL:url]; callbacksForURL = sself.URLCallbacks[url];
} if (finished) {
[sself.URLCallbacks removeObjectForKey:url];
}
});
for (NSDictionary *callbacks in callbacksForURL) { for (NSDictionary *callbacks in callbacksForURL) {
SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey]; SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
if (callback) callback(image, data, error, finished); if (callback) callback(image, data, error, finished);
@ -156,7 +162,9 @@ static NSString *const kCompletedCallbackKey = @"completed";
cancelled:^{ cancelled:^{
SDWebImageDownloader *sself = wself; SDWebImageDownloader *sself = wself;
if (!sself) return; if (!sself) return;
[sself removeCallbacksForURL:url]; dispatch_barrier_async(sself.barrierQueue, ^{
[sself.URLCallbacks removeObjectForKey:url];
});
}]; }];
if (wself.username && wself.password) { if (wself.username && wself.password) {
@ -210,20 +218,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
}); });
} }
- (NSArray *)callbacksForURL:(NSURL *)url {
__block NSArray *callbacksForURL;
dispatch_sync(self.barrierQueue, ^{
callbacksForURL = self.URLCallbacks[url];
});
return [callbacksForURL copy];
}
- (void)removeCallbacksForURL:(NSURL *)url {
dispatch_barrier_async(self.barrierQueue, ^{
[self.URLCallbacks removeObjectForKey:url];
});
}
- (void)setSuspended:(BOOL)suspended { - (void)setSuspended:(BOOL)suspended {
[self.downloadQueue setSuspended:suspended]; [self.downloadQueue setSuspended:suspended];
} }

View File

@ -4,7 +4,7 @@ xcodeproj 'SDWebImage Tests'
workspace '../SDWebImage' workspace '../SDWebImage'
def import_pods def import_pods
pod 'Expecta' # A Matcher Framework for Objective-C/Cocoa pod 'Expecta', '<=0.3.1' # A Matcher Framework for Objective-C/Cocoa
pod 'SDWebImage', :path => '../' pod 'SDWebImage', :path => '../'
end end