mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Migrate RKResponse property from RKObjectLoader to RKRequest. refs #527
This commit is contained in:
@@ -247,6 +247,11 @@ typedef void(^RKRequestDidFailLoadWithErrorBlock)(NSError *error);
|
||||
*/
|
||||
@property (nonatomic, readonly) NSString *HTTPMethod;
|
||||
|
||||
/**
|
||||
The response returned when the receiver was sent.
|
||||
*/
|
||||
@property (nonatomic, retain, readonly) RKResponse *response;
|
||||
|
||||
/**
|
||||
A serializable collection of parameters sent as the HTTP body of the request
|
||||
*/
|
||||
|
||||
@@ -89,6 +89,7 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
@property (nonatomic, assign, readwrite, getter = isLoaded) BOOL loaded;
|
||||
@property (nonatomic, assign, readwrite, getter = isLoading) BOOL loading;
|
||||
@property (nonatomic, assign, readwrite) BOOL canceled;
|
||||
@property (nonatomic, retain, readwrite) RKResponse *response;
|
||||
@end
|
||||
|
||||
@implementation RKRequest
|
||||
@@ -128,6 +129,7 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
@synthesize loaded = _loaded;
|
||||
@synthesize loading = _loading;
|
||||
@synthesize canceled = _canceled;
|
||||
@synthesize response = _response;
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
@synthesize backgroundPolicy = _backgroundPolicy;
|
||||
@@ -207,27 +209,29 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
if (_onDidLoadResponse) Block_release(_onDidLoadResponse);
|
||||
if (_onDidFailLoadWithError) Block_release(_onDidFailLoadWithError);
|
||||
|
||||
_delegate = nil;
|
||||
_delegate = nil;
|
||||
_configurationDelegate = nil;
|
||||
[_reachabilityObserver release];
|
||||
_reachabilityObserver = nil;
|
||||
[_connection cancel];
|
||||
[_connection release];
|
||||
_connection = nil;
|
||||
[_userData release];
|
||||
_userData = nil;
|
||||
[_URL release];
|
||||
_URL = nil;
|
||||
[_URLRequest release];
|
||||
_URLRequest = nil;
|
||||
[_params release];
|
||||
[_connection cancel];
|
||||
[_connection release];
|
||||
_connection = nil;
|
||||
[_response release];
|
||||
_response = nil;
|
||||
[_userData release];
|
||||
_userData = nil;
|
||||
[_URL release];
|
||||
_URL = nil;
|
||||
[_URLRequest release];
|
||||
_URLRequest = nil;
|
||||
[_params release];
|
||||
_params = nil;
|
||||
[_additionalHTTPHeaders release];
|
||||
_additionalHTTPHeaders = nil;
|
||||
[_username release];
|
||||
_username = nil;
|
||||
[_password release];
|
||||
_password = nil;
|
||||
[_additionalHTTPHeaders release];
|
||||
_additionalHTTPHeaders = nil;
|
||||
[_username release];
|
||||
_username = nil;
|
||||
[_password release];
|
||||
_password = nil;
|
||||
[_cache release];
|
||||
_cache = nil;
|
||||
[_OAuth1ConsumerKey release];
|
||||
@@ -325,7 +329,7 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add OAuth headers if is need it
|
||||
// Add OAuth headers if necessary
|
||||
// OAuth 1
|
||||
if(self.authenticationType == RKRequestAuthenticationTypeOAuth1){
|
||||
NSURLRequest *echo = nil;
|
||||
@@ -685,17 +689,17 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
[self.cache setCacheDate:date forRequest:self];
|
||||
}
|
||||
|
||||
- (void)didFinishLoad:(RKResponse*)response {
|
||||
self.loading = NO;
|
||||
self.loaded = YES;
|
||||
- (void)didFinishLoad:(RKResponse *)response {
|
||||
self.loading = NO;
|
||||
self.loaded = YES;
|
||||
|
||||
RKLogInfo(@"Status Code: %ld", (long) [response statusCode]);
|
||||
RKLogDebug(@"Body: %@", [response bodyAsString]);
|
||||
|
||||
RKResponse* finalResponse = response;
|
||||
self.response = response;
|
||||
|
||||
if ((_cachePolicy & RKRequestCachePolicyEtag) && [response isNotModified]) {
|
||||
finalResponse = [self loadResponseFromCache];
|
||||
self.response = [self loadResponseFromCache];
|
||||
[self updateInternalCacheDate];
|
||||
}
|
||||
|
||||
@@ -704,19 +708,18 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
|
||||
}
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
|
||||
[_delegate request:self didLoadResponse:finalResponse];
|
||||
[_delegate request:self didLoadResponse:self.response];
|
||||
}
|
||||
|
||||
if (self.onDidLoadResponse) {
|
||||
self.onDidLoadResponse(finalResponse);
|
||||
self.onDidLoadResponse(self.response);
|
||||
}
|
||||
|
||||
|
||||
if ([response isServiceUnavailable]) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKServiceDidBecomeUnavailableNotification object:self];
|
||||
}
|
||||
|
||||
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:finalResponse
|
||||
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:self.response
|
||||
forKey:RKRequestDidLoadResponseNotificationUserInfoResponseKey];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestDidLoadResponseNotification
|
||||
object:self
|
||||
|
||||
@@ -77,7 +77,7 @@
|
||||
/**
|
||||
The request that generated this response.
|
||||
*/
|
||||
@property (nonatomic, readonly) RKRequest *request;
|
||||
@property (nonatomic, assign, readonly) RKRequest *request;
|
||||
|
||||
/**
|
||||
The URL the response was loaded from.
|
||||
|
||||
@@ -36,7 +36,9 @@ return __VA_ARGS__;
|
||||
|
||||
@implementation RKResponse
|
||||
|
||||
@synthesize body = _body, request = _request, failureError = _failureError;
|
||||
@synthesize body = _body;
|
||||
@synthesize request = _request;
|
||||
@synthesize failureError = _failureError;
|
||||
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
@@ -50,7 +52,7 @@ return __VA_ARGS__;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)initWithRequest:(RKRequest*)request {
|
||||
- (id)initWithRequest:(RKRequest *)request {
|
||||
self = [self init];
|
||||
if (self) {
|
||||
// We don't retain here as we're letting RKRequestQueue manage
|
||||
|
||||
@@ -136,14 +136,8 @@ typedef void(^RKObjectLoaderDidLoadObjectsDictionaryBlock)(NSDictionary *diction
|
||||
* includes Core Data specific mapping logic.
|
||||
*/
|
||||
@interface RKObjectLoader : RKRequest {
|
||||
RKObjectMappingProvider *_mappingProvider;
|
||||
RKResponse* _response;
|
||||
RKObjectMapping* _objectMapping;
|
||||
RKObjectMappingResult* _result;
|
||||
RKObjectMapping* _serializationMapping;
|
||||
NSString* _serializationMIMEType;
|
||||
NSObject* _sourceObject;
|
||||
NSObject* _targetObject;
|
||||
id _sourceObject;
|
||||
id _targetObject;
|
||||
dispatch_queue_t _mappingQueue;
|
||||
}
|
||||
|
||||
@@ -213,7 +207,7 @@ typedef void(^RKObjectLoaderDidLoadObjectsDictionaryBlock)(NSDictionary *diction
|
||||
/**
|
||||
* The underlying response object for this loader
|
||||
*/
|
||||
@property (nonatomic, readonly) RKResponse *response;
|
||||
@property (nonatomic, retain, readonly) RKResponse *response;
|
||||
|
||||
/**
|
||||
* The mapping result that was produced after the request finished loading and
|
||||
|
||||
@@ -40,12 +40,12 @@
|
||||
@interface RKObjectLoader ()
|
||||
@property (nonatomic, assign, readwrite, getter = isLoaded) BOOL loaded;
|
||||
@property (nonatomic, assign, readwrite, getter = isLoading) BOOL loading;
|
||||
@property (nonatomic, retain, readwrite) RKResponse *response;
|
||||
@end
|
||||
|
||||
@implementation RKObjectLoader
|
||||
|
||||
@synthesize mappingProvider = _mappingProvider;
|
||||
@synthesize response = _response;
|
||||
@synthesize targetObject = _targetObject;
|
||||
@synthesize objectMapping = _objectMapping;
|
||||
@synthesize result = _result;
|
||||
@@ -59,6 +59,7 @@
|
||||
@synthesize onDidLoadObjectsDictionary = _onDidLoadObjectsDictionary;
|
||||
@dynamic loaded;
|
||||
@dynamic loading;
|
||||
@dynamic response;
|
||||
|
||||
+ (id)loaderWithURL:(RKURL *)URL mappingProvider:(RKObjectMappingProvider *)mappingProvider {
|
||||
return [[[self alloc] initWithURL:URL mappingProvider:mappingProvider] autorelease];
|
||||
@@ -80,9 +81,7 @@
|
||||
[_sourceObject release];
|
||||
_sourceObject = nil;
|
||||
[_targetObject release];
|
||||
_targetObject = nil;
|
||||
[_response release];
|
||||
_response = nil;
|
||||
_targetObject = nil;
|
||||
[_objectMapping release];
|
||||
_objectMapping = nil;
|
||||
[_result release];
|
||||
@@ -105,8 +104,6 @@
|
||||
|
||||
- (void)reset {
|
||||
[super reset];
|
||||
[_response release];
|
||||
_response = nil;
|
||||
[_result release];
|
||||
_result = nil;
|
||||
}
|
||||
@@ -421,30 +418,28 @@
|
||||
// NOTE: We do NOT call super here. We are overloading the default behavior from RKRequest
|
||||
- (void)didFinishLoad:(RKResponse*)response {
|
||||
NSAssert([NSThread isMainThread], @"RKObjectLoaderDelegate callbacks must occur on the main thread");
|
||||
_response = [response retain];
|
||||
self.response = response;
|
||||
|
||||
if ((_cachePolicy & RKRequestCachePolicyEtag) && [response isNotModified]) {
|
||||
[_response release];
|
||||
_response = nil;
|
||||
_response = [[self.cache responseForRequest:self] retain];
|
||||
NSAssert(_response, @"Unexpectedly loaded nil response from cache");
|
||||
self.response = [self.cache responseForRequest:self];
|
||||
NSAssert(self.response, @"Unexpectedly loaded nil response from cache");
|
||||
[self updateInternalCacheDate];
|
||||
}
|
||||
|
||||
if (![_response wasLoadedFromCache] && [_response isSuccessful] && (_cachePolicy != RKRequestCachePolicyNone)) {
|
||||
[self.cache storeResponse:_response forRequest:self];
|
||||
if (![self.response wasLoadedFromCache] && [self.response isSuccessful] && (_cachePolicy != RKRequestCachePolicyNone)) {
|
||||
[self.cache storeResponse:self.response forRequest:self];
|
||||
}
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
|
||||
[_delegate request:self didLoadResponse:_response];
|
||||
[_delegate request:self didLoadResponse:self.response];
|
||||
}
|
||||
|
||||
if (self.onDidLoadResponse) {
|
||||
self.onDidLoadResponse(_response);
|
||||
self.onDidLoadResponse(self.response);
|
||||
}
|
||||
|
||||
// Post the notification
|
||||
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:_response
|
||||
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:self.response
|
||||
forKey:RKRequestDidLoadResponseNotificationUserInfoResponseKey];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKRequestDidLoadResponseNotification
|
||||
object:self
|
||||
|
||||
Reference in New Issue
Block a user