mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
change RKRequest delegate didfinishload method signature to be a bit cleaner; added code to turn off the reachability observer when the app becomes inactive; changed online/offline state code to support an undetermined state at initial launch, so as to ensure we get all our proper notifications during first run; remove unnecessary debug logging from rkrequestqueue; removed three20 model code that was triggering a forced offline state, which currently does not have a recovery path (e.g. if the app is forced offline, we currently have no built-in mechanism to allow a force back online)
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import "RKReachabilityObserver.h"
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
// Constants
|
||||
NSString* const RKReachabilityStateChangedNotification = @"RKReachabilityStateChangedNotification";
|
||||
@@ -44,7 +45,6 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
|
||||
if (nil != reachabilityRef) {
|
||||
observer = [[[self alloc] initWithReachabilityRef:reachabilityRef] autorelease];
|
||||
}
|
||||
|
||||
return observer;
|
||||
}
|
||||
|
||||
@@ -52,17 +52,25 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
|
||||
if (self = [self init]) {
|
||||
_reachabilityRef = reachabilityRef;
|
||||
[self scheduleObserver];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(scheduleObserver)
|
||||
name:UIApplicationDidBecomeActiveNotification
|
||||
object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(unscheduleObserver)
|
||||
name:UIApplicationWillResignActiveNotification
|
||||
object:nil];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
[self unscheduleObserver];
|
||||
if (_reachabilityRef) {
|
||||
CFRelease(_reachabilityRef);
|
||||
}
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
@@ -101,7 +109,6 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
|
||||
status = RKReachabilityReachableViaWWAN;
|
||||
}
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -115,7 +122,6 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
|
||||
if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) {
|
||||
return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ typedef enum RKRequestMethod {
|
||||
/**
|
||||
* Sent when a request has finished loading
|
||||
*/
|
||||
- (void)requestDidFinishLoad:(RKRequest*)request withResponse:(RKResponse*)response;
|
||||
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response;
|
||||
|
||||
/**
|
||||
* Sent when a request has failed due to an error
|
||||
|
||||
@@ -207,8 +207,8 @@
|
||||
_isLoading = NO;
|
||||
_isLoaded = YES;
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(requestDidFinishLoad:withResponse:)]) {
|
||||
[_delegate requestDidFinishLoad:self withResponse:response];
|
||||
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
|
||||
[_delegate request:self didLoadResponse:response];
|
||||
}
|
||||
|
||||
NSDate* receivedAt = [NSDate date];
|
||||
|
||||
@@ -100,10 +100,6 @@ static const NSInteger kMaxConcurrentLoads = 5;
|
||||
|
||||
- (void)sendRequest:(RKRequest*)request {
|
||||
[_requests addObject:request];
|
||||
|
||||
NSLog(@"Request added: URL=%@, request=%@, delegate=%@",
|
||||
[request URL], request, [request delegate]);
|
||||
|
||||
[self loadNextInQueue];
|
||||
}
|
||||
|
||||
@@ -112,9 +108,6 @@ static const NSInteger kMaxConcurrentLoads = 5;
|
||||
[request cancel];
|
||||
request.delegate = nil;
|
||||
|
||||
NSLog(@"Request cancelled and removed: URL=%@, request=%@, delegate=%@",
|
||||
[request URL], request, [request delegate]);
|
||||
|
||||
[_requests removeObject:request];
|
||||
_totalLoading--;
|
||||
|
||||
@@ -153,21 +146,12 @@ static const NSInteger kMaxConcurrentLoads = 5;
|
||||
// Our RKRequest completed and we're notified with an RKResponse object
|
||||
if ([notification.object isKindOfClass:[RKResponse class]]) {
|
||||
RKRequest* request = [(RKResponse*)notification.object request];
|
||||
|
||||
NSLog(@"Request completed and removed: URL=%@, request=%@, delegate=%@",
|
||||
[request URL], request, [request delegate]);
|
||||
|
||||
[_requests removeObject:request];
|
||||
_totalLoading--;
|
||||
|
||||
// Our RKRequest failed and we're notified with the original RKRequest object
|
||||
} else if ([notification.object isKindOfClass:[RKRequest class]]) {
|
||||
RKRequest* request = (RKRequest*)notification.object;
|
||||
|
||||
NSError* error = (NSError*)[notification.userInfo objectForKey:@"error"];
|
||||
NSLog(@"Request failed and removed: URL=%@, error=%@, request=%@, delegate=%@",
|
||||
[request URL], error, request, [request delegate]);
|
||||
|
||||
[_requests removeObject:request];
|
||||
_totalLoading--;
|
||||
}
|
||||
|
||||
@@ -240,8 +240,8 @@
|
||||
- (void)didFinishLoad:(RKResponse*)response {
|
||||
_response = [response retain];
|
||||
|
||||
if ([_delegate respondsToSelector:@selector(requestDidFinishLoad:withResponse:)]) {
|
||||
[_delegate requestDidFinishLoad:self withResponse:response];
|
||||
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
|
||||
[_delegate request:self didLoadResponse:response];
|
||||
}
|
||||
|
||||
if (NO == [self encounteredErrorWhileProcessingRequest:response]) {
|
||||
|
||||
@@ -15,6 +15,12 @@
|
||||
extern NSString* const RKDidEnterOfflineModeNotification;
|
||||
extern NSString* const RKDidEnterOnlineModeNotification;
|
||||
|
||||
typedef enum {
|
||||
RKObjectManagerOnlineStateUndetermined,
|
||||
RKObjectManagerOnlineStateDisconnected,
|
||||
RKObjectManagerOnlineStateConnected
|
||||
} RKObjectManagerOnlineState;
|
||||
|
||||
// TODO: Factor out into a protocol...
|
||||
// insertObject:, deleteObject:, save, etc.
|
||||
@class RKManagedObjectStore;
|
||||
@@ -24,8 +30,8 @@ extern NSString* const RKDidEnterOnlineModeNotification;
|
||||
RKMappingFormat _format;
|
||||
RKObjectMapper* _mapper;
|
||||
NSObject<RKRouter>* _router;
|
||||
RKManagedObjectStore* _objectStore;
|
||||
BOOL _isOnline;
|
||||
RKManagedObjectStore* _objectStore;
|
||||
RKObjectManagerOnlineState _onlineState;
|
||||
BOOL _onlineStateForced;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ static RKObjectManager* sharedManager = nil;
|
||||
_router = [[RKDynamicRouter alloc] init];
|
||||
_client = [[RKClient clientWithBaseURL:baseURL] retain];
|
||||
self.format = RKMappingFormatJSON;
|
||||
_isOnline = YES;
|
||||
_onlineState = RKObjectManagerOnlineStateUndetermined;
|
||||
_onlineStateForced = NO;
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self
|
||||
selector:@selector(reachabilityChanged:)
|
||||
@@ -86,19 +86,19 @@ static RKObjectManager* sharedManager = nil;
|
||||
}
|
||||
|
||||
- (void)goOffline {
|
||||
_isOnline = NO;
|
||||
_onlineState = RKObjectManagerOnlineStateDisconnected;
|
||||
_onlineStateForced = YES;
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKDidEnterOfflineModeNotification object:self];
|
||||
}
|
||||
|
||||
- (void)goOnline {
|
||||
_isOnline = YES;
|
||||
_onlineState = RKObjectManagerOnlineStateConnected;
|
||||
_onlineStateForced = YES;
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKDidEnterOnlineModeNotification object:self];
|
||||
}
|
||||
|
||||
- (BOOL)isOnline {
|
||||
return _isOnline;
|
||||
return (_onlineState == RKObjectManagerOnlineStateConnected);
|
||||
}
|
||||
|
||||
- (BOOL)isOffline {
|
||||
@@ -108,14 +108,14 @@ static RKObjectManager* sharedManager = nil;
|
||||
- (void)reachabilityChanged:(NSNotification*)notification {
|
||||
if (!_onlineStateForced) {
|
||||
BOOL isHostReachable = [self.client.baseURLReachabilityObserver isNetworkReachable];
|
||||
BOOL isOnline = _isOnline;
|
||||
BOOL onlineState = _onlineState;
|
||||
|
||||
_isOnline = isHostReachable;
|
||||
_onlineState = isHostReachable ? RKObjectManagerOnlineStateConnected : RKObjectManagerOnlineStateDisconnected;
|
||||
|
||||
if (isOnline && !isHostReachable) {
|
||||
if ((onlineState == RKObjectManagerOnlineStateConnected || onlineState == RKObjectManagerOnlineStateUndetermined) && !isHostReachable) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKDidEnterOfflineModeNotification object:self];
|
||||
|
||||
} else if (!isOnline && isHostReachable) {
|
||||
} else if ((onlineState == RKObjectManagerOnlineStateDisconnected || onlineState == RKObjectManagerOnlineStateUndetermined) && isHostReachable) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RKDidEnterOnlineModeNotification object:self];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,9 +170,9 @@ static NSString* const kDefaultLoadedTimeKey = @"RKRequestTTModelDefaultLoadedTi
|
||||
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
|
||||
_isLoading = NO;
|
||||
[self didFailLoadWithError:error];
|
||||
if ([self errorWarrantsOptionToGoOffline:error]) {
|
||||
[self showAlertWithOptionToGoOfflineForError:error];
|
||||
}
|
||||
// if ([self errorWarrantsOptionToGoOffline:error]) {
|
||||
// [self showAlertWithOptionToGoOfflineForError:error];
|
||||
// }
|
||||
}
|
||||
|
||||
- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader*)objectLoader {
|
||||
|
||||
Reference in New Issue
Block a user