Fix the issue when using the `sd_cancelCurrentImageLoad` on non-stateful view (like UIImageView.image) (#3653)

This commit is contained in:
DreamPiggy 2023-12-27 21:13:08 +08:00 committed by GitHub
parent e278c13e46
commit b10d4a245a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 34 deletions

View File

@ -20,14 +20,16 @@
* *
* @param key key for identifying the operations * @param key key for identifying the operations
* @return the image load operation * @return the image load operation
* @note If key is nil, means using the NSStringFromClass(self.class) instead, match the behavior of `operation key`
*/ */
- (nullable id<SDWebImageOperation>)sd_imageLoadOperationForKey:(nullable NSString *)key; - (nullable id<SDWebImageOperation>)sd_imageLoadOperationForKey:(nullable NSString *)key;
/** /**
* Set the image load operation (storage in a UIView based weak map table) * Set the image load operation (storage in a UIView based weak map table)
* *
* @param operation the operation * @param operation the operation, should not be nil or no-op will perform
* @param key key for storing the operation * @param key key for storing the operation
* @note If key is nil, means using the NSStringFromClass(self.class) instead, match the behavior of `operation key`
*/ */
- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key; - (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key;
@ -35,13 +37,15 @@
* Cancel the operation for the current UIView and key * Cancel the operation for the current UIView and key
* *
* @param key key for identifying the operations * @param key key for identifying the operations
* @note If key is nil, means using the NSStringFromClass(self.class) instead, match the behavior of `operation key`
*/ */
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key; - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key;
/** /**
* Just remove the operation corresponding to the current UIView and key without cancelling them * Just remove the operation corresponding to the current UIView and key without cancelling them
* *
* @param key key for identifying the operations * @param key key for identifying the operations.
* @note If key is nil, means using the NSStringFromClass(self.class) instead, match the behavior of `operation key`
*/ */
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key; - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;

View File

@ -29,53 +29,57 @@ typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;
- (nullable id<SDWebImageOperation>)sd_imageLoadOperationForKey:(nullable NSString *)key { - (nullable id<SDWebImageOperation>)sd_imageLoadOperationForKey:(nullable NSString *)key {
id<SDWebImageOperation> operation; id<SDWebImageOperation> operation;
if (key) { if (!key) {
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; key = NSStringFromClass(self.class);
@synchronized (self) { }
operation = [operationDictionary objectForKey:key]; SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
} @synchronized (self) {
operation = [operationDictionary objectForKey:key];
} }
return operation; return operation;
} }
- (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key { - (void)sd_setImageLoadOperation:(nullable id<SDWebImageOperation>)operation forKey:(nullable NSString *)key {
if (key) { if (!key) {
if (operation) { key = NSStringFromClass(self.class);
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; }
@synchronized (self) { if (operation) {
[operationDictionary setObject:operation forKey:key]; SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
} @synchronized (self) {
[operationDictionary setObject:operation forKey:key];
} }
} }
} }
- (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key { - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key {
if (key) { if (!key) {
// Cancel in progress downloader from queue key = NSStringFromClass(self.class);
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
id<SDWebImageOperation> operation;
@synchronized (self) {
operation = [operationDictionary objectForKey:key];
}
if (operation) {
if ([operation respondsToSelector:@selector(cancel)]) {
[operation cancel];
}
@synchronized (self) {
[operationDictionary removeObjectForKey:key];
}
}
} }
} // Cancel in progress downloader from queue
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key { id<SDWebImageOperation> operation;
if (key) {
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; @synchronized (self) {
operation = [operationDictionary objectForKey:key];
}
if (operation) {
if ([operation respondsToSelector:@selector(cancel)]) {
[operation cancel];
}
@synchronized (self) { @synchronized (self) {
[operationDictionary removeObjectForKey:key]; [operationDictionary removeObjectForKey:key];
} }
} }
} }
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key {
if (!key) {
key = NSStringFromClass(self.class);
}
SDOperationsDictionary *operationDictionary = [self sd_operationDictionary];
@synchronized (self) {
[operationDictionary removeObjectForKey:key];
}
}
@end @end