diff --git a/SDWebImage/Core/UIView+WebCacheOperation.h b/SDWebImage/Core/UIView+WebCacheOperation.h index fc23508b..0bc12caf 100644 --- a/SDWebImage/Core/UIView+WebCacheOperation.h +++ b/SDWebImage/Core/UIView+WebCacheOperation.h @@ -20,14 +20,16 @@ * * @param key key for identifying the operations * @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)sd_imageLoadOperationForKey:(nullable NSString *)key; /** * 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 + * @note If key is nil, means using the NSStringFromClass(self.class) instead, match the behavior of `operation key` */ - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key; @@ -35,13 +37,15 @@ * Cancel the operation for the current UIView and key * * @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; /** * 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; diff --git a/SDWebImage/Core/UIView+WebCacheOperation.m b/SDWebImage/Core/UIView+WebCacheOperation.m index ec4d9dd1..99c8fe45 100644 --- a/SDWebImage/Core/UIView+WebCacheOperation.m +++ b/SDWebImage/Core/UIView+WebCacheOperation.m @@ -29,53 +29,57 @@ typedef NSMapTable> SDOperationsDictionary; - (nullable id)sd_imageLoadOperationForKey:(nullable NSString *)key { id operation; - if (key) { - SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; - @synchronized (self) { - operation = [operationDictionary objectForKey:key]; - } + if (!key) { + key = NSStringFromClass(self.class); + } + SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; + @synchronized (self) { + operation = [operationDictionary objectForKey:key]; } return operation; } - (void)sd_setImageLoadOperation:(nullable id)operation forKey:(nullable NSString *)key { - if (key) { - if (operation) { - SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; - @synchronized (self) { - [operationDictionary setObject:operation forKey:key]; - } + if (!key) { + key = NSStringFromClass(self.class); + } + if (operation) { + SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; + @synchronized (self) { + [operationDictionary setObject:operation forKey:key]; } } } - (void)sd_cancelImageLoadOperationWithKey:(nullable NSString *)key { - if (key) { - // Cancel in progress downloader from queue - SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; - id operation; - - @synchronized (self) { - operation = [operationDictionary objectForKey:key]; - } - if (operation) { - if ([operation respondsToSelector:@selector(cancel)]) { - [operation cancel]; - } - @synchronized (self) { - [operationDictionary removeObjectForKey:key]; - } - } + if (!key) { + key = NSStringFromClass(self.class); } -} - -- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key { - if (key) { - SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; + // Cancel in progress downloader from queue + SDOperationsDictionary *operationDictionary = [self sd_operationDictionary]; + id operation; + + @synchronized (self) { + operation = [operationDictionary objectForKey:key]; + } + if (operation) { + if ([operation respondsToSelector:@selector(cancel)]) { + [operation cancel]; + } @synchronized (self) { [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