Skip to content

Commit

Permalink
Merge c013589 into 4e2bd2b
Browse files Browse the repository at this point in the history
  • Loading branch information
8W9aG committed Feb 2, 2016
2 parents 4e2bd2b + c013589 commit bbc3eba
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
18 changes: 16 additions & 2 deletions SPTDataLoader/SPTDataLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "SPTDataLoaderResponse.h"
#import "SPTDataLoaderRequestResponseHandler.h"
#import "SPTDataLoaderDelegate.h"
#import "SPTDataLoaderResponse+Private.h"

@interface SPTDataLoader ()

Expand Down Expand Up @@ -71,9 +72,22 @@ - (void)executeDelegateBlock:(dispatch_block_t)block
{
SPTDataLoaderRequest *copiedRequest = [request copy];
id<SPTDataLoaderDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(dataLoaderShouldSupportChunks:)] && copiedRequest.chunks) {
NSAssert([delegate dataLoaderShouldSupportChunks:self], @"The data loader was given a request that required chunks while the delegate does not support chunks");

// Cancel the request immediately if it requires chunks and the delegate does not support that
BOOL chunksSupported = [delegate respondsToSelector:@selector(dataLoaderShouldSupportChunks:)];
if (chunksSupported) {
chunksSupported = [delegate dataLoaderShouldSupportChunks:self];
}
if (!chunksSupported && copiedRequest.chunks) {
NSError *error = [NSError errorWithDomain:SPTDataLoaderRequestErrorDomain
code:SPTDataLoaderRequestErrorChunkedRequestWithoutChunkedDelegate
userInfo:nil];
SPTDataLoaderResponse *response = [SPTDataLoaderResponse dataLoaderResponseWithRequest:request response:nil];
response.error = error;
[delegate dataLoader:self didReceiveErrorResponse:response];
return nil;
}

id<SPTCancellationToken> cancellationToken = [self.requestResponseHandlerDelegate requestResponseHandler:self
performRequest:copiedRequest];
@synchronized(self.cancellationTokens) {
Expand Down
8 changes: 8 additions & 0 deletions SPTDataLoaderTests/SPTDataLoaderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,12 @@ - (void)testDelegateCallbackOnSeparateQueue
XCTAssertEqual(self.delegate.numberOfCallsToSuccessfulResponse, 1u, @"The data loader did not relay a successful response to the delegate");
}

- (void)testErrorDelegateCallbackWhenMismatchInChunkSupport
{
SPTDataLoaderRequest *request = [SPTDataLoaderRequest new];
request.chunks = YES;
[self.dataLoader performRequest:request];
XCTAssertEqual(self.delegate.numberOfCallsToErrorResponse, 1u);
}

@end
3 changes: 2 additions & 1 deletion include/SPTDataLoader/SPTDataLoaderRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ typedef NS_ENUM(NSInteger, SPTDataLoaderRequestMethod) {
};

typedef NS_ENUM(NSInteger, SPTDataLoaderRequestErrorCode) {
SPTDataLoaderRequestErrorCodeTimeout
SPTDataLoaderRequestErrorCodeTimeout,
SPTDataLoaderRequestErrorChunkedRequestWithoutChunkedDelegate
};

extern NSString * const SPTDataLoaderRequestErrorDomain;
Expand Down

0 comments on commit bbc3eba

Please sign in to comment.