Преглед изворни кода

Replace all the weak-strong dance code with @weakify and @strongify

DreamPiggy пре 7 година
родитељ
комит
e31a44f6a1

+ 16 - 10
SDWebImage/SDImageCachesManager.m

@@ -8,6 +8,7 @@
 
 #import "SDImageCachesManager.h"
 #import "SDImageCachesManagerOperation.h"
+#import "SDInternalMacros.h"
 
 @implementation SDImageCachesManager
 
@@ -407,8 +408,9 @@
         }
         return;
     }
-    __weak typeof(self) wself = self;
+    @weakify(self);
     [cache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
+        @strongify(self);
         if (operation.isCancelled) {
             // Cancelled
             return;
@@ -427,7 +429,7 @@
             return;
         }
         // Next
-        [wself serialQueryImageForKey:key options:options context:context completion:completionBlock enumerator:enumerator operation:operation];
+        [self serialQueryImageForKey:key options:options context:context completion:completionBlock enumerator:enumerator operation:operation];
     }];
 }
 
@@ -441,10 +443,11 @@
         }
         return;
     }
-    __weak typeof(self) wself = self;
+    @weakify(self);
     [cache storeImage:image imageData:imageData forKey:key cacheType:cacheType completion:^{
+        @strongify(self);
         // Next
-        [wself serialStoreImage:image imageData:imageData forKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator];
+        [self serialStoreImage:image imageData:imageData forKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator];
     }];
 }
 
@@ -458,10 +461,11 @@
         }
         return;
     }
-    __weak typeof(self) wself = self;
+    @weakify(self);
     [cache removeImageForKey:key cacheType:cacheType completion:^{
+        @strongify(self);
         // Next
-        [wself serialRemoveImageForKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator];
+        [self serialRemoveImageForKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator];
     }];
 }
 
@@ -477,8 +481,9 @@
         }
         return;
     }
-    __weak typeof(self) wself = self;
+    @weakify(self);
     [cache containsImageForKey:key cacheType:cacheType completion:^(SDImageCacheType containsCacheType) {
+        @strongify(self);
         if (operation.isCancelled) {
             // Cancelled
             return;
@@ -497,7 +502,7 @@
             return;
         }
         // Next
-        [wself serialContainsImageForKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator operation:operation];
+        [self serialContainsImageForKey:key cacheType:cacheType completion:completionBlock enumerator:enumerator operation:operation];
     }];
 }
 
@@ -511,10 +516,11 @@
         }
         return;
     }
-    __weak typeof(self) wself = self;
+    @weakify(self);
     [cache clearWithCacheType:cacheType completion:^{
+        @strongify(self);
         // Next
-        [wself serialClearWithCacheType:cacheType completion:completionBlock enumerator:enumerator];
+        [self serialClearWithCacheType:cacheType completion:completionBlock enumerator:enumerator];
     }];
 }
 

+ 6 - 6
SDWebImage/SDWebImageDownloader.m

@@ -203,15 +203,15 @@ static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
             }
             return nil;
         }
-        __weak typeof(self) wself = self;
+        @weakify(self);
         operation.completionBlock = ^{
-            __strong typeof(wself) sself = wself;
-            if (!sself) {
+            @strongify(self);
+            if (!self) {
                 return;
             }
-            SD_LOCK(sself.operationsLock);
-            [sself.URLOperations removeObjectForKey:url];
-            SD_UNLOCK(sself.operationsLock);
+            SD_LOCK(self.operationsLock);
+            [self.URLOperations removeObjectForKey:url];
+            SD_UNLOCK(self.operationsLock);
         };
         self.URLOperations[url] = operation;
         // Add operation to operation queue only after all configuration done according to Apple's doc.

+ 1 - 1
SDWebImage/SDWebImageDownloaderOperation.m

@@ -142,7 +142,7 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
         Class UIApplicationClass = NSClassFromString(@"UIApplication");
         BOOL hasApplication = UIApplicationClass && [UIApplicationClass respondsToSelector:@selector(sharedApplication)];
         if (hasApplication && [self shouldContinueWhenAppEntersBackground]) {
-            __weak __typeof__ (self) wself = self;
+            __weak typeof(self) wself = self;
             UIApplication * app = [UIApplicationClass performSelector:@selector(sharedApplication)];
             self.backgroundTaskId = [app beginBackgroundTaskWithExpirationHandler:^{
                 [wself cancel];

+ 11 - 11
SDWebImage/SDWebImageManager.m

@@ -189,15 +189,15 @@ static id<SDImageLoader> _defaultImageLoader;
     if (shouldQueryCache) {
         id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
         NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
-        __weak SDWebImageCombinedOperation *weakOperation = operation;
+        @weakify(operation);
         operation.cacheOperation = [self.imageCache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) {
-            __strong __typeof(weakOperation) strongOperation = weakOperation;
-            if (!strongOperation || strongOperation.isCancelled) {
-                [self safelyRemoveOperationFromRunning:strongOperation];
+            @strongify(operation);
+            if (!operation || operation.isCancelled) {
+                [self safelyRemoveOperationFromRunning:operation];
                 return;
             }
             // Continue download process
-            [self callDownloadProcessForOperation:strongOperation url:url options:options context:context cachedImage:cachedImage cachedData:cachedData cacheType:cacheType progress:progressBlock completed:completedBlock];
+            [self callDownloadProcessForOperation:operation url:url options:options context:context cachedImage:cachedImage cachedData:cachedData cacheType:cacheType progress:progressBlock completed:completedBlock];
         }];
     } else {
         // Continue download process
@@ -237,17 +237,17 @@ static id<SDImageLoader> _defaultImageLoader;
         }
         
         // `SDWebImageCombinedOperation` -> `SDWebImageDownloadToken` -> `downloadOperationCancelToken`, which is a `SDCallbacksDictionary` and retain the completed block below, so we need weak-strong again to avoid retain cycle
-        __weak typeof(operation) weakOperation = operation;
+        @weakify(operation);
         operation.loaderOperation = [self.imageLoader loadImageWithURL:url options:options context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
-            __strong typeof(weakOperation) strongOperation = weakOperation;
-            if (!strongOperation || strongOperation.isCancelled) {
+            @strongify(operation);
+            if (!operation || operation.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 (cachedImage && options & SDWebImageRefreshCached && [error.domain isEqualToString:SDWebImageErrorDomain] && error.code == SDWebImageErrorCacheNotModified) {
                 // Image refresh hit the NSURLCache cache, do not call the completion block
             } else if (error) {
-                [self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
+                [self callCompletionBlockForOperation:operation completion:completedBlock error:error url:url];
                 BOOL shouldBlockFailedURL = [self shouldBlockFailedURLWithURL:url error:error];
                 
                 if (shouldBlockFailedURL) {
@@ -262,11 +262,11 @@ static id<SDImageLoader> _defaultImageLoader;
                     SD_UNLOCK(self.failedURLsLock);
                 }
                 
-                [self callStoreCacheProcessForOperation:strongOperation url:url options:options context:context downloadedImage:downloadedImage downloadedData:downloadedData finished:finished progress:progressBlock completed:completedBlock];
+                [self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:downloadedImage downloadedData:downloadedData finished:finished progress:progressBlock completed:completedBlock];
             }
             
             if (finished) {
-                [self safelyRemoveOperationFromRunning:strongOperation];
+                [self safelyRemoveOperationFromRunning:operation];
             }
         }];
     } else if (cachedImage) {

+ 14 - 13
SDWebImage/UIView+WebCache.m

@@ -10,6 +10,7 @@
 #import "objc/runtime.h"
 #import "UIView+WebCacheOperation.h"
 #import "SDWebImageError.h"
+#import "SDInternalMacros.h"
 
 const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
 
@@ -73,10 +74,10 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
             manager = [SDWebImageManager sharedManager];
         }
         
-        __weak __typeof(self)wself = self;
+        @weakify(self);
         SDImageLoaderProgressBlock combinedProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
-            __strong __typeof (wself) sself = wself;
-            NSProgress *imageProgress = sself.sd_imageProgress;
+            @strongify(self);
+            NSProgress *imageProgress = self.sd_imageProgress;
             imageProgress.totalUnitCount = expectedSize;
             imageProgress.completedUnitCount = receivedSize;
 #if SD_UIKIT || SD_MAC
@@ -92,12 +93,12 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
             }
         };
         id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
-            __strong __typeof (wself) sself = wself;
-            if (!sself) { return; }
+            @strongify(self);
+            if (!self) { return; }
             // if the progress not been updated, mark it to complete state
-            if (finished && !error && sself.sd_imageProgress.totalUnitCount == 0 && sself.sd_imageProgress.completedUnitCount == 0) {
-                sself.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
-                sself.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
+            if (finished && !error && self.sd_imageProgress.totalUnitCount == 0 && self.sd_imageProgress.completedUnitCount == 0) {
+                self.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
+                self.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
             }
             
 #if SD_UIKIT || SD_MAC
@@ -111,9 +112,9 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
             BOOL shouldNotSetImage = ((image && (options & SDWebImageAvoidAutoSetImage)) ||
                                       (!image && !(options & SDWebImageDelayPlaceholder)));
             SDWebImageNoParamsBlock callCompletedBlockClojure = ^{
-                if (!sself) { return; }
+                if (!self) { return; }
                 if (!shouldNotSetImage) {
-                    [sself sd_setNeedsLayout];
+                    [self sd_setNeedsLayout];
                 }
                 if (completedBlock && shouldCallCompletedBlock) {
                     completedBlock(image, data, error, cacheType, finished, url);
@@ -144,14 +145,14 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
             // check whether we should use the image transition
             SDWebImageTransition *transition = nil;
             if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) {
-                transition = sself.sd_imageTransition;
+                transition = self.sd_imageTransition;
             }
 #endif
             dispatch_main_async_safe(^{
 #if SD_UIKIT || SD_MAC
-                [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
+                [self sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
 #else
-                [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock cacheType:cacheType imageURL:imageURL];
+                [self sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock cacheType:cacheType imageURL:imageURL];
 #endif
                 callCompletedBlockClojure();
             });