From 2424848c9266b54c5e0c723b684dc702524ef66c Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 27 Dec 2023 19:21:06 +0800 Subject: [PATCH] Formally deprecate `sd_cancelCurrentImageLoad`, which cause misunderstanding on UIImageView category Use `sd_cancelLatestImageLoad` instead --- SDWebImage/Core/UIView+WebCache.h | 17 ++++++++++++----- SDWebImage/Core/UIView+WebCache.m | 4 ++++ Tests/Tests/SDWebCacheCategoriesTests.m | 6 +++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/SDWebImage/Core/UIView+WebCache.h b/SDWebImage/Core/UIView+WebCache.h index c15edaa7..2223f9d9 100644 --- a/SDWebImage/Core/UIView+WebCache.h +++ b/SDWebImage/Core/UIView+WebCache.h @@ -91,12 +91,19 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima completed:(nullable SDInternalCompletionBlock)completedBlock; /** - * Cancel the current image load - * This simply translate to `[self sd_cancelImageLoadOperationWithKey:self.sd_latestOperationKey]` from v5.18.0 - * - * @warning This method should be only used for single state view, like `UIImageView` without highlighted state. For stateful view like `UIBUtton` (one view can have multiple images loading), use `sd_cancelImageLoadOperationWithKey:` instead. See `UIView+WebCacheOperation.h` for more information. + * Cancel the latest image load, using the `sd_latestOperationKey` as operation key + * This simply translate to `[self sd_cancelImageLoadOperationWithKey:self.sd_latestOperationKey]` */ -- (void)sd_cancelCurrentImageLoad; +- (void)sd_cancelLatestImageLoad; + +/** + * Cancel the current image load, for single state view. + * This actually does not cancel current loading, because stateful view can load multiple images at the same time (like UIButton, each state can load different images). Just behave the same as `sd_cancelLatestImageLoad` + * + * @warning This method should be only used for single state view, like `UIImageView` without highlighted state. For stateful view like `UIBUtton` (one view can have multiple images loading), use `sd_cancelImageLoadOperationWithKey:` instead. See `UIView+WebCacheOperation.h` for more information. + * @deprecated Use `sd_cancelLatestImageLoad` instead. Which don't cause overload method misunderstanding (`UIImageView+WebCache` provide the same API as this one, but does not do the same thing). This API will be totally removed in v6.0 due to this. + */ +- (void)sd_cancelCurrentImageLoad API_DEPRECATED_WITH_REPLACEMENT("sd_cancelLatestImageLoad", macos(10.10, 10.10), ios(8.0, 8.0), tvos(9.0, 9.0), watchos(2.0, 2.0)); #if SD_UIKIT || SD_MAC diff --git a/SDWebImage/Core/UIView+WebCache.m b/SDWebImage/Core/UIView+WebCache.m index b9c0e045..23d0d739 100644 --- a/SDWebImage/Core/UIView+WebCache.m +++ b/SDWebImage/Core/UIView+WebCache.m @@ -255,6 +255,10 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; return operation; } +- (void)sd_cancelLatestImageLoad { + [self sd_cancelImageLoadOperationWithKey:self.sd_latestOperationKey]; +} + - (void)sd_cancelCurrentImageLoad { [self sd_cancelImageLoadOperationWithKey:self.sd_latestOperationKey]; } diff --git a/Tests/Tests/SDWebCacheCategoriesTests.m b/Tests/Tests/SDWebCacheCategoriesTests.m index 94859f37..49061809 100644 --- a/Tests/Tests/SDWebCacheCategoriesTests.m +++ b/Tests/Tests/SDWebCacheCategoriesTests.m @@ -334,7 +334,7 @@ [SDImageCache.sharedImageCache removeImageFromDiskForKey:kTestJPEGURL]; [SDImageCache.sharedImageCache removeImageFromMemoryForKey:kTestJPEGURL]; SDWebImageCombinedOperation *op1 = [imageView sd_internalSetImageWithURL:originalImageURL placeholderImage:nil options:0 context:nil setImageBlock:nil progress:nil completed:nil]; - [imageView sd_cancelCurrentImageLoad]; + [imageView sd_cancelLatestImageLoad]; expect(op1.isCancelled).beTruthy(); NSString *operationKey = NSStringFromClass(UIView.class); expect([imageView sd_imageLoadOperationForKey:operationKey]).beNil(); @@ -393,7 +393,7 @@ // At this point, our transition has started, and so we cancel the load operation, // perhaps as a result of a call to `prepareForReuse` in a UICollectionViewCell - [imageView sd_cancelCurrentImageLoad]; + [imageView sd_cancelLatestImageLoad]; // Now, we update our context's imageOperationKey and URL, perhaps // because of a re-use of a UICollectionViewCell. In this case, @@ -423,7 +423,7 @@ expect(error.code).equal(SDWebImageErrorCancelled); [expectation fulfill]; }]; - [imageView sd_cancelCurrentImageLoad]; + [imageView sd_cancelLatestImageLoad]; [self waitForExpectationsWithCommonTimeout]; }