diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 1c008d0c..c6aa6269 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -135,7 +135,10 @@ static NSString *const kCompletedCallbackKey = @"completed"; progress:^(NSInteger receivedSize, NSInteger expectedSize) { SDWebImageDownloader *sself = wself; if (!sself) return; - NSArray *callbacksForURL = [sself callbacksForURL:url]; + __block NSArray *callbacksForURL; + dispatch_sync(sself.barrierQueue, ^{ + callbacksForURL = sself.URLCallbacks[url]; + }); for (NSDictionary *callbacks in callbacksForURL) { SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey]; if (callback) callback(receivedSize, expectedSize); @@ -144,10 +147,13 @@ static NSString *const kCompletedCallbackKey = @"completed"; completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { SDWebImageDownloader *sself = wself; if (!sself) return; - NSArray *callbacksForURL = [sself callbacksForURL:url]; - if (finished) { - [sself removeCallbacksForURL:url]; - } + __block NSArray *callbacksForURL; + dispatch_barrier_sync(sself.barrierQueue, ^{ + callbacksForURL = sself.URLCallbacks[url]; + if (finished) { + [sself.URLCallbacks removeObjectForKey:url]; + } + }); for (NSDictionary *callbacks in callbacksForURL) { SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey]; if (callback) callback(image, data, error, finished); @@ -156,7 +162,9 @@ static NSString *const kCompletedCallbackKey = @"completed"; cancelled:^{ SDWebImageDownloader *sself = wself; if (!sself) return; - [sself removeCallbacksForURL:url]; + dispatch_barrier_async(sself.barrierQueue, ^{ + [sself.URLCallbacks removeObjectForKey:url]; + }); }]; 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 { [self.downloadQueue setSuspended:suspended]; } diff --git a/Tests/Podfile b/Tests/Podfile index 7ac80ded..7f86b807 100644 --- a/Tests/Podfile +++ b/Tests/Podfile @@ -4,7 +4,7 @@ xcodeproj 'SDWebImage Tests' workspace '../SDWebImage' 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 => '../' end