Add support for spinning the Network Activity Indicator while object request operations are executing. Add notifications for tracking object request operation lifecycle.

This commit is contained in:
Blake Watters
2012-10-17 22:35:13 -04:00
parent 68cebc96af
commit 384ff80845
2 changed files with 55 additions and 7 deletions

View File

@@ -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;

View File

@@ -24,10 +24,33 @@
#import "RKHTTPUtilities.h"
#import "RKLog.h"
#import <Availability.h>
#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