Skip to content

Commit

Permalink
Create a strong ref of weakOperation in the entry of The image downlo…
Browse files Browse the repository at this point in the history
…ad subOperation, use the strong ref instead weakOperation
  • Loading branch information
yirenjun committed Oct 26, 2015
1 parent 6b5f323 commit 2b57abd
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions SDWebImage/SDWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ - (void)diskImageExistsForURL:(NSURL *)url
downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
}
id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) {
if (weakOperation.isCancelled) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (!strongOperation || strongOperation.isCancelled) {
// Do nothing if the operation was cancelled
// See #699 for more details
// if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data
}
else if (error) {
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(nil, error, SDImageCacheTypeNone, finished, url);
}
});
Expand Down Expand Up @@ -226,7 +227,7 @@ - (void)diskImageExistsForURL:(NSURL *)url
}

dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url);
}
});
Expand All @@ -238,7 +239,7 @@ - (void)diskImageExistsForURL:(NSURL *)url
}

dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url);
}
});
Expand All @@ -247,21 +248,27 @@ - (void)diskImageExistsForURL:(NSURL *)url

if (finished) {
@synchronized (self.runningOperations) {
[self.runningOperations removeObject:operation];
if (strongOperation) {
[self.runningOperations removeObject:strongOperation];
}
}
}
}];
operation.cancelBlock = ^{
[subOperation cancel];

@synchronized (self.runningOperations) {
[self.runningOperations removeObject:weakOperation];
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (strongOperation) {
[self.runningOperations removeObject:strongOperation];
}
}
};
}
else if (image) {
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (strongOperation && !strongOperation.isCancelled) {
completedBlock(image, nil, cacheType, YES, url);
}
});
Expand All @@ -272,7 +279,8 @@ - (void)diskImageExistsForURL:(NSURL *)url
else {
// Image not in cache and download disallowed by delegate
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
if (strongOperation && !weakOperation.isCancelled) {
completedBlock(nil, nil, SDImageCacheTypeNone, YES, url);
}
});
Expand Down

0 comments on commit 2b57abd

Please sign in to comment.