diff --git a/Code/Network/RKObjectRequestOperation.h b/Code/Network/RKObjectRequestOperation.h index c28e6a36..b50e8f38 100644 --- a/Code/Network/RKObjectRequestOperation.h +++ b/Code/Network/RKObjectRequestOperation.h @@ -159,3 +159,17 @@ // TODO: Need tests for: success, request failure, request timeout, parsing failure, no matching mapping descriptors, parsing an error out of the payload, // no mappable content found, unable to parse the MIME type returned, handling a 204 response, getting back a 200 with 'blank' content (i.e. render :nothing => true) @end + +///-------------------- +/// @name Notifications +///-------------------- + +/** + Posted when an object request operation begin executing. + */ +extern NSString * const RKObjectRequestOperationDidStartNotification; + +/** + Posted when an object request operation finishes. + */ +extern NSString * const RKObjectRequestOperationDidFinishNotification; diff --git a/Code/Network/RKObjectRequestOperation.m b/Code/Network/RKObjectRequestOperation.m index 51e6925a..fabadd13 100644 --- a/Code/Network/RKObjectRequestOperation.m +++ b/Code/Network/RKObjectRequestOperation.m @@ -24,10 +24,33 @@ #import "RKHTTPUtilities.h" #import "RKLog.h" +#import + +#if __IPHONE_OS_VERSION_MIN_REQUIRED +#import "AFNetworkActivityIndicatorManager.h" +#endif + // Set Logging Component #undef RKLogComponent #define RKLogComponent RKlcl_cRestKitNetwork +NSString * const RKObjectRequestOperationDidStartNotification = @"RKObjectRequestOperationDidStartNotification"; +NSString * const RKObjectRequestOperationDidFinishNotification = @"RKObjectRequestOperationDidFinishNotification"; + +static void RKIncrementNetworkActivityIndicator() +{ + #if __IPHONE_OS_VERSION_MIN_REQUIRED + [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount]; + #endif +} + +static void RKDecrementNetworkAcitivityIndicator() +{ + #if __IPHONE_OS_VERSION_MIN_REQUIRED + [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount]; + #endif +} + static inline NSString *RKDescriptionForRequest(NSURLRequest *request) { return [NSString stringWithFormat:@"%@ '%@'", request.HTTPMethod, [request.URL absoluteString]]; @@ -175,14 +198,12 @@ static NSIndexSet *RKObjectRequestOperationAcceptableMIMETypes() [self.HTTPRequestOperation cancel]; } -- (void)main +- (void)execute { - if (self.isCancelled) return; - // Send the request [self.HTTPRequestOperation start]; [self.HTTPRequestOperation waitUntilFinished]; - + if (self.HTTPRequestOperation.error) { RKLogError(@"Object request failed: Underlying HTTP request operation failed with error: %@", self.HTTPRequestOperation.error); self.error = self.HTTPRequestOperation.error; @@ -190,12 +211,14 @@ static NSIndexSet *RKObjectRequestOperationAcceptableMIMETypes() } if (self.isCancelled) return; - + // Map the response NSError *error; RKMappingResult *mappingResult = [self performMappingOnResponse:&error]; - if (self.isCancelled) return; - + if (self.isCancelled) { + return; + } + // If there is no mapping result but no error, there was no mapping to be performed, // which we do not treat as an error condition if (! mappingResult && error) { @@ -206,4 +229,15 @@ static NSIndexSet *RKObjectRequestOperationAcceptableMIMETypes() [self willFinish]; } +- (void)main +{ + if (self.isCancelled) return; + + [[NSNotificationCenter defaultCenter] postNotificationName:RKObjectRequestOperationDidStartNotification object:self]; + RKIncrementNetworkActivityIndicator(); + [self execute]; + RKDecrementNetworkAcitivityIndicator(); + [[NSNotificationCenter defaultCenter] postNotificationName:RKObjectRequestOperationDidFinishNotification object:self]; +} + @end