[Issue #236] Adding AFURLConnectionOperation -setCacheResponseBlock:, which allows the behavior of the NSURLConnectionDelegate method -connection:willCacheResponse: to be overridden without subclassing

This commit is contained in:
Mattt Thompson 2012-03-09 13:50:46 -08:00
parent 467de06532
commit ce2063f06d
2 changed files with 25 additions and 6 deletions

View file

@ -192,7 +192,7 @@ extern NSString * const AFNetworkingOperationDidFinishNotification;
- (void)setDownloadProgressBlock:(void (^)(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead))block; - (void)setDownloadProgressBlock:(void (^)(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead))block;
///------------------------------------------------- ///-------------------------------------------------
/// @name Setting Authentication Challenge Callbacks /// @name Setting NSURLConnection Delegate Callbacks
///------------------------------------------------- ///-------------------------------------------------
/** /**
@ -213,4 +213,11 @@ extern NSString * const AFNetworkingOperationDidFinishNotification;
*/ */
- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block; - (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block;
/**
Sets a block to be executed to modify the response a connection will cache, if any, as handled by the `NSURLConnectionDelegate` method `connection:willCacheResponse:`.
@param block A block object to be executed to determine what response a connection will cache, if any. The block returns an `NSCachedURLResponse` object, the cached response to store in memory or `nil` to prevent the response from being cached, and takes two arguments: the URL connection object, and the cached response provided for the request.
*/
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block;
@end @end

View file

@ -43,6 +43,7 @@ NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.ne
typedef void (^AFURLConnectionOperationProgressBlock)(NSInteger bytes, NSInteger totalBytes, NSInteger totalBytesExpected); typedef void (^AFURLConnectionOperationProgressBlock)(NSInteger bytes, NSInteger totalBytes, NSInteger totalBytesExpected);
typedef BOOL (^AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace); typedef BOOL (^AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock)(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace);
typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge); typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge);
typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse);
static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { static inline NSString * AFKeyPathFromOperationState(AFOperationState state) {
switch (state) { switch (state) {
@ -97,6 +98,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress; @property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress;
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock authenticationAgainstProtectionSpace; @property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationAgainstProtectionSpaceBlock authenticationAgainstProtectionSpace;
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge; @property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge;
@property (readwrite, nonatomic, copy) AFURLConnectionOperationCacheResponseBlock cacheResponse;
- (void)operationDidStart; - (void)operationDidStart;
- (void)finish; - (void)finish;
@ -119,6 +121,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
@synthesize downloadProgress = _downloadProgress; @synthesize downloadProgress = _downloadProgress;
@synthesize authenticationAgainstProtectionSpace = _authenticationAgainstProtectionSpace; @synthesize authenticationAgainstProtectionSpace = _authenticationAgainstProtectionSpace;
@synthesize authenticationChallenge = _authenticationChallenge; @synthesize authenticationChallenge = _authenticationChallenge;
@synthesize cacheResponse = _cacheResponse;
@synthesize lock = _lock; @synthesize lock = _lock;
+ (void)networkRequestThreadEntryPoint:(id)__unused object { + (void)networkRequestThreadEntryPoint:(id)__unused object {
@ -191,6 +194,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
[_downloadProgress release]; [_downloadProgress release];
[_authenticationChallenge release]; [_authenticationChallenge release];
[_authenticationAgainstProtectionSpace release]; [_authenticationAgainstProtectionSpace release];
[_cacheResponse release];
[_connection release]; [_connection release];
@ -241,6 +245,10 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
self.authenticationChallenge = block; self.authenticationChallenge = block;
} }
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block {
self.cacheResponse = block;
}
- (void)setState:(AFOperationState)state { - (void)setState:(AFOperationState)state {
[self.lock lock]; [self.lock lock];
if (AFStateTransitionIsValid(self.state, state, [self isCancelled])) { if (AFStateTransitionIsValid(self.state, state, [self isCancelled])) {
@ -491,14 +499,18 @@ didReceiveResponse:(NSURLResponse *)response
self.connection = nil; self.connection = nil;
} }
- (NSCachedURLResponse *)connection:(NSURLConnection *)__unused connection - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse willCacheResponse:(NSCachedURLResponse *)cachedResponse
{ {
if (self.cacheResponse) {
return self.cacheResponse(connection, cachedResponse);
} else {
if ([self isCancelled]) { if ([self isCancelled]) {
return nil; return nil;
} }
return cachedResponse; return cachedResponse;
}
} }
@end @end