Merge pull request #2856 from dreampiggy/fix_retain_cancel_token

Make the SDWebImageDownloadToken to not retain the completion block so that user don't need weak-strong dance
This commit is contained in:
DreamPiggy 2019-10-01 11:28:06 +08:00 committed by GitHub
commit c294c0c6a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 27 deletions

View File

@ -24,9 +24,8 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
@property (nonatomic, strong, nullable, readwrite) NSURL *url;
@property (nonatomic, strong, nullable, readwrite) NSURLRequest *request;
@property (nonatomic, strong, nullable, readwrite) NSURLResponse *response;
@property (nonatomic, strong, nullable, readwrite) id downloadOperationCancelToken;
@property (nonatomic, weak, nullable, readwrite) id downloadOperationCancelToken;
@property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperation> *downloadOperation;
@property (nonatomic, weak, nullable) SDWebImageDownloader *downloader;
@property (nonatomic, assign, getter=isCancelled) BOOL cancelled;
- (nonnull instancetype)init NS_UNAVAILABLE;
@ -253,7 +252,6 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
token.url = url;
token.request = operation.request;
token.downloadOperationCancelToken = downloadOperationCancelToken;
token.downloader = self;
return token;
}
@ -331,22 +329,6 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
return operation;
}
- (void)cancel:(nullable SDWebImageDownloadToken *)token {
NSURL *url = token.url;
if (!url) {
return;
}
SD_LOCK(self.operationsLock);
NSOperation<SDWebImageDownloaderOperation> *operation = [self.URLOperations objectForKey:url];
if (operation) {
BOOL canceled = [operation cancel:token.downloadOperationCancelToken];
if (canceled) {
[self.URLOperations removeObjectForKey:url];
}
}
SD_UNLOCK(self.operationsLock);
}
- (void)cancelAllDownloads {
[self.downloadQueue cancelAllOperations];
}
@ -506,13 +488,7 @@ didReceiveResponse:(NSURLResponse *)response
return;
}
self.cancelled = YES;
if (self.downloader) {
// Downloader is alive, cancel token
[self.downloader cancel:self];
} else {
// Downloader is dealloced, only cancel download operation
[self.downloadOperation cancel:self.downloadOperationCancelToken];
}
[self.downloadOperation cancel:self.downloadOperationCancelToken];
self.downloadOperationCancelToken = nil;
}
}

View File

@ -238,7 +238,6 @@ static id<SDImageLoader> _defaultImageLoader;
context = [mutableContext copy];
}
// `SDWebImageCombinedOperation` -> `SDWebImageDownloadToken` -> `downloadOperationCancelToken`, which is a `SDCallbacksDictionary` and retain the completed block below, so we need weak-strong again to avoid retain cycle
@weakify(operation);
operation.loaderOperation = [self.imageLoader requestImageWithURL:url options:options context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
@strongify(operation);