Merge pull request #1046 from hoppenichu/fix_downloader
Fix SDWebImageDownloader threading issue
This commit is contained in:
commit
0f157d4332
|
@ -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];
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue