mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user