Skip to content

Commit

Permalink
Merge pull request #519 from zhongwuzw/add_session_complete_delegate
Browse files Browse the repository at this point in the history
Exposure didCompleteTask:withError: delegate method of protocol PINURLSessionManagerDelegate
  • Loading branch information
ernestmama committed Nov 9, 2019
2 parents ebe69fb + c946515 commit 5e51b81
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [new] Add PINRemoteImageManagerConfiguration configuration object. [#492](https://github.com/pinterest/PINRemoteImage/pull/492) [rqueue](https://github.com/rqueue)
- [fixed] Fixes blending in animated WebP images. [#507](https://github.com/pinterest/PINRemoteImage/pull/507) [garrettmoon](https://github.com/garrettmoon)
- [fixed] Fixes support in PINAnimatedImageView for WebP animated images. [#507](https://github.com/pinterest/PINRemoteImage/pull/507) [garrettmoon](https://github.com/garrettmoon)
- [new] Exposure didCompleteTask:withError: delegate method of protocol PINURLSessionManagerDelegate. [#519](https://github.com/pinterest/PINRemoteImage/pull/519) [zhongwuzw](https://github.com/zhongwuzw)
- [fixed] Fixes AnimatedImageView designated initializer not work. [#512](https://github.com/pinterest/PINRemoteImage/pull/512) [zhongwuzw](https://github.com/zhongwuzw)
- [fixed] Set bpp(bits per pixel) to 32 bit for GIF. [#511](https://github.com/pinterest/PINRemoteImage/pull/511) [zhongwuzw](https://github.com/zhongwuzw)
- [new] Add cancel method for PINRemoteImageManager. [#509](https://github.com/pinterest/PINRemoteImage/pull/509) [zhongwuzw](https://github.com/zhongwuzw)
Expand Down
2 changes: 1 addition & 1 deletion Source/Classes/PINURLSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extern NSErrorDomain _Nonnull const PINURLErrorDomain;
- (void)didCollectMetrics:(nonnull NSURLSessionTaskMetrics *)metrics forURL:(nonnull NSURL *)url API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
- (void)didReceiveResponse:(nonnull NSURLResponse *)response forTask:(nonnull NSURLSessionTask *)task;
- (void)didReceiveAuthenticationChallenge:(nonnull NSURLAuthenticationChallenge *)challenge forTask:(nullable NSURLSessionTask *)task completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;

- (void)didCompleteTask:(nonnull NSURLSessionTask *)task withError:(nullable NSError *)error;

@end

Expand Down
4 changes: 4 additions & 0 deletions Source/Classes/PINURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
if (completionHandler) {
completionHandler(task, error);
}

if ([strongSelf.delegate respondsToSelector:@selector(didCompleteTask:withError:)]) {
[strongSelf.delegate didCompleteTask:task withError:error];
}
});
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/PINRemoteImageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ @interface PINRemoteImage_Tests : XCTestCase <PINURLSessionManagerDelegate>
@property (nonatomic, strong) NSMutableData *data;
@property (nonatomic, strong) NSURLSessionTask *task;
@property (nonatomic, strong) NSError *error;
@property (nonatomic, strong) dispatch_semaphore_t sessionDidCompleteDelegateSemaphore;

@end

Expand Down Expand Up @@ -222,6 +223,10 @@ - (void)didCompleteTask:(NSURLSessionTask *)task withError:(NSError *)error
{
self.task = task;
self.error = error;
// Used for URLSessionDidCompleteDelegate test
if (self.sessionDidCompleteDelegateSemaphore) {
dispatch_semaphore_signal(self.sessionDidCompleteDelegateSemaphore);
}
}

- (void)setUp
Expand All @@ -239,6 +244,9 @@ - (void)tearDown
self.imageManager = nil;
[[PINSpeedRecorder sharedRecorder] setCurrentBytesPerSecond:-1];
[[PINSpeedRecorder sharedRecorder] resetMeasurements];
if (self.sessionDidCompleteDelegateSemaphore) {
self.sessionDidCompleteDelegateSemaphore = nil;
}
[super tearDown];
}

Expand Down Expand Up @@ -1410,6 +1418,16 @@ - (void)testRetry
method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (void)testURLSessionDidCompleteDelegate
{
self.imageManager = [[PINRemoteImageManager alloc] init];
self.imageManager.sessionManager.delegate = self;
self.sessionDidCompleteDelegateSemaphore = dispatch_semaphore_create(0);
[self.imageManager downloadImageWithURL:[self transparentPNGURL] completion:nil];
long result = dispatch_semaphore_wait(self.sessionDidCompleteDelegateSemaphore, [self timeout]);
XCTAssert(result == 0);
}

- (void)testCancelAllTasks
{
XCTestExpectation *expectation = [self expectationWithDescription:@"Cancel all tasks"];
Expand Down

0 comments on commit 5e51b81

Please sign in to comment.