Skip to content

Commit

Permalink
Adds support for getting NSURLResponse in result object. (#382)
Browse files Browse the repository at this point in the history
* Adds support for getting NSURLResponse in result object.

* Add changelog entry

* Fixing spacing
  • Loading branch information
garrettmoon authored Jun 29, 2017
1 parent 11f195f commit fce52ea
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 92 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [fixed] Fixes a threadsafety issue in accessing callbacks. [garrettmoon](https://github.com/garrettmoon)
- [new] PINRemoteImageManager now respects the request timeout value of session configuration. [garrettmoon](https://github.com/garrettmoon)
- [new] Updated to latest PINCache beta 5. [garrettmoon](https://github.com/garrettmoon)
- [new] Added support for getting NSURLResponse from a PINRemoteImageManagerResult object. [garrettmoon](https://github.com/garrettmoon)

## 3.0.0 Beta 10
- [new] Added support (in iOS 10) for skipping cancelation if the estimated amount of time to complete the download is less than the average time to first byte for a host. [#364](https://github.com/pinterest/PINRemoteImage/pull/364) [garrettmoon](https://github.com/garrettmoon)
Expand Down
9 changes: 6 additions & 3 deletions Source/Classes/PINRemoteImageDownloadTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ - (void)callProgressImageWithImage:(nonnull PINImage *)image renderedImageQualit
progressImageBlock([PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:nil
requestLength:CACurrentMediaTime() - requestTime
error:nil
resultType:PINRemoteImageResultTypeProgress
UUID:UUID
response:nil
error:nil
renderedImageQuality:renderedImageQuality]);
});
}
Expand Down Expand Up @@ -156,6 +157,7 @@ - (nonnull PINRemoteImageManagerResult *)imageResultWithImage:(nullable PINImage
error:(nullable NSError *)error
resultType:(PINRemoteImageResultType)resultType
UUID:(nullable NSUUID *)UUID
response:(nonnull NSURLResponse *)response
{
__block NSUInteger bytesSavedByResuming;
[self.lock lockWithBlock:^{
Expand All @@ -164,9 +166,10 @@ - (nonnull PINRemoteImageManagerResult *)imageResultWithImage:(nullable PINImage
return [PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:requestLength
error:error
resultType:resultType
UUID:UUID
response:response
error:error
bytesSavedByResuming:bytesSavedByResuming];
}

Expand Down Expand Up @@ -337,7 +340,7 @@ - (void)scheduleDownloadWithRequest:(NSURLRequest *)request
}
}

completionHandler(data, error);
completionHandler(data, response, error);
}
}];
}]];
Expand Down
2 changes: 1 addition & 1 deletion Source/Classes/PINRemoteImageManager+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#import "PINRemoteImageDownloadQueue.h"

typedef void (^PINRemoteImageManagerDataCompletion)(NSData *data, NSError *error);
typedef void (^PINRemoteImageManagerDataCompletion)(NSData *data, NSURLResponse *response, NSError *error);

@interface PINRemoteImageManager (private)

Expand Down
4 changes: 3 additions & 1 deletion Source/Classes/PINRemoteImageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,12 @@ typedef void(^PINRemoteImageManagerProgressDownload)(int64_t completedBytes, int
/**
* Sets a custom header to be included in every request. Headers set from this method will override any header from NSURLSessionConfiguration.
*
* @deprecated Use NSURLSessionConfiguration.HTTPAdditionalHeaders instead
* @param value A value for the header. Pass in nil to remove a previously set value.
* @param header A string field for header.
*/
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)header;
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)header __attribute__((deprecated));

/**
Sets the Request Configuration Block.
Expand Down
59 changes: 32 additions & 27 deletions Source/Classes/PINRemoteImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ float dataTaskPriorityWithImageManagerPriority(PINRemoteImageManagerPriority pri
NSString * const PINRemoteImageManagerErrorDomain = @"PINRemoteImageManagerErrorDomain";
NSString * const PINRemoteImageCacheKey = @"cacheKey";
NSString * const PINRemoteImageCacheKeyResumePrefix = @"R-";
typedef void (^PINRemoteImageManagerDataCompletion)(NSData *data, NSError *error);
typedef void (^PINRemoteImageManagerDataCompletion)(NSData *data, NSURLResponse *response, NSError *error);

@interface PINTaskQOS : NSObject

Expand Down Expand Up @@ -622,7 +622,7 @@ - (NSUUID *)downloadImageWithURL:(NSURL *)url
if (found) {
if (valid) {
typeof(self) strongSelf = weakSelf;
[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:alternativeRepresentation cached:YES error:nil finalized:YES];
[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:alternativeRepresentation cached:YES response:nil error:nil finalized:YES];
} else {
//Remove completion and try again
typeof(self) strongSelf = weakSelf;
Expand Down Expand Up @@ -713,8 +713,8 @@ - (void)downloadImageWithURL:(NSURL *)url
code:PINRemoteImageManagerErrorFailedToProcessImage
userInfo:nil];
}
[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:nil cached:NO error:error finalized:NO];
[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:nil cached:NO response:result.response error:error finalized:NO];

if (error == nil && image != nil) {
BOOL saveAsJPEG = (options & PINRemoteImageManagerSaveProcessedImageAsJPEG) != 0;
NSData *diskData = nil;
Expand All @@ -727,15 +727,15 @@ - (void)downloadImageWithURL:(NSURL *)url
[strongSelf materializeAndCacheObject:image cacheInDisk:diskData additionalCost:processCost url:url key:key options:options outImage:nil outAltRep:nil];
}

[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:nil cached:NO error:error finalized:YES];
[strongSelf callCompletionsWithKey:key image:image alternativeRepresentation:nil cached:NO response:result.response error:error finalized:YES];
} else {
if (error == nil) {
error = [NSError errorWithDomain:PINRemoteImageManagerErrorDomain
code:PINRemoteImageManagerErrorFailedToFetchImageForProcessing
userInfo:nil];
}

[strongSelf callCompletionsWithKey:key image:nil alternativeRepresentation:nil cached:NO error:error finalized:YES];
[strongSelf callCompletionsWithKey:key image:nil alternativeRepresentation:nil cached:NO response:result.response error:error finalized:YES];
}
}];
task.downloadTaskUUID = downloadTaskUUID;
Expand All @@ -762,7 +762,7 @@ - (void)downloadImageWithURL:(NSURL *)url
resume:resume
skipRetry:(options & PINRemoteImageManagerDownloadOptionsSkipRetry)
priority:priority
completionHandler:^(NSData *data, NSError *error)
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
[_concurrentOperationQueue addOperation:^
{
Expand All @@ -782,7 +782,7 @@ - (void)downloadImageWithURL:(NSURL *)url
userInfo:nil];
}

[self callCompletionsWithKey:key image:image alternativeRepresentation:alternativeRepresentation cached:NO error:remoteImageError finalized:YES];
[self callCompletionsWithKey:key image:image alternativeRepresentation:alternativeRepresentation cached:NO response:response error:remoteImageError finalized:YES];
} withPriority:operationPriorityWithImageManagerPriority(priority)];
}];
}
Expand Down Expand Up @@ -827,21 +827,18 @@ - (BOOL)earlyReturnWithOptions:(PINRemoteImageManagerDownloadOptions)options url
code:NSURLErrorUnsupportedURL
userInfo:@{ NSLocalizedDescriptionKey : @"unsupported URL" }];
}
PINRemoteImageManagerResult *result = [PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:0
resultType:resultType
UUID:nil
response:nil
error:error];
if (allowEarlyReturn && [NSThread isMainThread]) {
completion([PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:0
error:error
resultType:resultType
UUID:nil]);
completion(result);
} else {
dispatch_async(self.callbackQueue, ^{
completion([PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:0
error:error
resultType:resultType
UUID:nil]);
completion(result);
});
}
return YES;
Expand Down Expand Up @@ -932,7 +929,7 @@ - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url
userInfo:nil];
}

completion(data, error);
completion(data, response, error);
}
}];

Expand All @@ -943,11 +940,17 @@ - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url
return dataTask;
}

- (void)callCompletionsWithKey:(NSString *)key image:(PINImage *)image alternativeRepresentation:(id)alternativeRepresentation cached:(BOOL)cached error:(NSError *)error finalized:(BOOL)finalized
- (void)callCompletionsWithKey:(NSString *)key
image:(PINImage *)image
alternativeRepresentation:(id)alternativeRepresentation
cached:(BOOL)cached
response:(NSURLResponse *)response
error:(NSError *)error
finalized:(BOOL)finalized
{
[self lock];
PINRemoteImageDownloadTask *task = [self.tasks objectForKey:key];
[task callCompletionsWithImage:image alternativeRepresentation:alternativeRepresentation cached:cached error:error remove:!finalized];
[task callCompletionsWithImage:image alternativeRepresentation:alternativeRepresentation cached:cached response:response error:error remove:!finalized];
if (finalized) {
[self.tasks removeObjectForKey:key];
}
Expand Down Expand Up @@ -1117,9 +1120,10 @@ - (void)imageFromCacheWithURL:(NSURL *)url
completion([PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:CACurrentMediaTime() - requestTime
error:error
resultType:PINRemoteImageResultTypeCache
UUID:nil]);
UUID:nil
response:nil
error:error]);
});
}];
}
Expand Down Expand Up @@ -1160,9 +1164,10 @@ - (PINRemoteImageManagerResult *)synchronousImageFromCacheWithURL:(NSURL *)url p
return [PINRemoteImageManagerResult imageResultWithImage:image
alternativeRepresentation:alternativeRepresentation
requestLength:CACurrentMediaTime() - requestTime
error:error
resultType:PINRemoteImageResultTypeMemoryCache
UUID:nil];
UUID:nil
response:nil
error:error];
}

#pragma mark - Session Task Blocks
Expand Down
12 changes: 8 additions & 4 deletions Source/Classes/PINRemoteImageManagerResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,32 @@ typedef NS_ENUM(NSUInteger, PINRemoteImageResultType) {
@property (nonatomic, readonly, strong, nullable) NSUUID *UUID;
@property (nonatomic, readonly, assign) CGFloat renderedImageQuality;
@property (nonatomic, readonly, assign) NSUInteger bytesSavedByResuming;
@property (nonatomic, readonly, strong, nullable) NSURLResponse *response;

+ (nonnull instancetype)imageResultWithImage:(nullable PINImage *)image
alternativeRepresentation:(nullable id)alternativeRepresentation
requestLength:(NSTimeInterval)requestLength
error:(nullable NSError *)error
resultType:(PINRemoteImageResultType)resultType
UUID:(nullable NSUUID *)uuid;
UUID:(nullable NSUUID *)uuid
response:(nullable NSURLResponse *)response
error:(nullable NSError *)error;

+ (nonnull instancetype)imageResultWithImage:(nullable PINImage *)image
alternativeRepresentation:(nullable id)alternativeRepresentation
requestLength:(NSTimeInterval)requestLength
error:(nullable NSError *)error
resultType:(PINRemoteImageResultType)resultType
UUID:(nullable NSUUID *)uuid
response:(nullable NSURLResponse *)response
error:(nullable NSError *)error
bytesSavedByResuming:(NSUInteger)bytesSavedByResuming;

+ (nonnull instancetype)imageResultWithImage:(nullable PINImage *)image
alternativeRepresentation:(nullable id)alternativeRepresentation
requestLength:(NSTimeInterval)requestLength
error:(nullable NSError *)error
resultType:(PINRemoteImageResultType)resultType
UUID:(nullable NSUUID *)uuid
response:(nullable NSURLResponse *)response
error:(nullable NSError *)error
renderedImageQuality:(CGFloat)renderedImageQuality;

@end
Loading

0 comments on commit fce52ea

Please sign in to comment.