Skip to content

Commit 6c5b928

Browse files
dtorresgarrettmoon
authored andcommitted
Generate the cacheKey only when necessary (#191)
1 parent b4a6bcb commit 6c5b928

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Pod/Classes/PINRemoteImageManager.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ float dataTaskPriorityWithImageManagerPriority(PINRemoteImageManagerPriority pri
8181
}
8282

8383
NSString * const PINRemoteImageManagerErrorDomain = @"PINRemoteImageManagerErrorDomain";
84+
NSString * const PINRemoteImageCacheKey = @"cacheKey";
8485
typedef void (^PINRemoteImageManagerDataCompletion)(NSData *data, NSError *error);
8586

8687
@interface NSOperationQueue (PINRemoteImageManager)
@@ -788,9 +789,10 @@ - (PINDataTaskOperation *)downloadDataWithURL:(NSURL *)url
788789
priority:(PINRemoteImageManagerPriority)priority
789790
completion:(PINRemoteImageManagerDataCompletion)completion
790791
{
791-
NSURLRequest *request = [NSURLRequest requestWithURL:url
792+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
792793
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
793794
timeoutInterval:self.timeout];
795+
[NSURLProtocol setProperty:key forKey:PINRemoteImageCacheKey inRequest:request];
794796

795797
__weak typeof(self) weakSelf = self;
796798
PINDataTaskOperation *dataTaskOperation = [PINDataTaskOperation dataTaskOperationWithSessionManager:self.sessionManager
@@ -1061,7 +1063,8 @@ - (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challe
10611063
- (void)didReceiveData:(NSData *)data forTask:(NSURLSessionDataTask *)dataTask
10621064
{
10631065
[self lock];
1064-
PINRemoteImageDownloadTask *task = [self.tasks objectForKey:[self cacheKeyForURL:[[dataTask originalRequest] URL] processorKey:nil]];
1066+
NSString *cacheKey = [NSURLProtocol propertyForKey:PINRemoteImageCacheKey inRequest:dataTask.originalRequest];
1067+
PINRemoteImageDownloadTask *task = [self.tasks objectForKey:cacheKey];
10651068
if (task.progressImage == nil) {
10661069
task.progressImage = [[PINProgressiveImage alloc] init];
10671070
task.progressImage.startTime = task.sessionTaskStartTime;
@@ -1089,7 +1092,6 @@ - (void)didReceiveData:(NSData *)data forTask:(NSURLSessionDataTask *)dataTask
10891092
PINImage *progressImage = [progressiveImage currentImageBlurred:shouldBlur maxProgressiveRenderSize:maxProgressiveRenderSize renderedImageQuality:&renderedImageQuality];
10901093
if (progressImage) {
10911094
[strongSelf lock];
1092-
NSString *cacheKey = [strongSelf cacheKeyForURL:[[dataTask originalRequest] URL] processorKey:nil];
10931095
PINRemoteImageDownloadTask *task = strongSelf.tasks[cacheKey];
10941096
[task callProgressImageWithQueue:strongSelf.callbackQueue withImage:progressImage renderedImageQuality:renderedImageQuality];
10951097
[strongSelf unlock];
@@ -1103,7 +1105,7 @@ - (void)didCompleteTask:(NSURLSessionTask *)task withError:(NSError *)error
11031105
if (error == nil && [task isKindOfClass:[NSURLSessionDataTask class]]) {
11041106
NSURLSessionDataTask *dataTask = (NSURLSessionDataTask *)task;
11051107
[self lock];
1106-
NSString *cacheKey = [self cacheKeyForURL:[[dataTask originalRequest] URL] processorKey:nil];
1108+
NSString *cacheKey = [NSURLProtocol propertyForKey:PINRemoteImageCacheKey inRequest:dataTask.originalRequest];
11071109
PINRemoteImageDownloadTask *task = [self.tasks objectForKey:cacheKey];
11081110
task.sessionTaskEndTime = CACurrentMediaTime();
11091111
CFTimeInterval taskLength = task.sessionTaskEndTime - task.sessionTaskStartTime;

0 commit comments

Comments
 (0)