mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 20:31:13 +08:00
moved logic for initiating a call to the server based on an empty local cache from initial load code path to the reload code path, thus simplifying this use case when in offline mode
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#import "../RestKit.h"
|
||||
|
||||
/**
|
||||
* Generic class for loading a remote model using a RestKit request and supplying the model to a
|
||||
* Generic class for loading a remote model using a RestKit request and supplying the model to a
|
||||
* TTListDataSource subclass
|
||||
*/
|
||||
@interface RKRequestTTModel : TTModel <RKObjectLoaderDelegate> {
|
||||
@@ -18,6 +18,7 @@
|
||||
BOOL _isLoaded;
|
||||
BOOL _isLoading;
|
||||
BOOL _cacheLoaded;
|
||||
BOOL _emptyReloadAttempted;
|
||||
|
||||
NSString* _resourcePath;
|
||||
NSDictionary* _params;
|
||||
|
||||
@@ -101,6 +101,7 @@ static NSString* const kDefaultLoadedTimeKey = @"RKRequestTTModelDefaultLoadedTi
|
||||
_isLoaded = NO;
|
||||
_isLoading = NO;
|
||||
_resourcePath = nil;
|
||||
_emptyReloadAttempted = NO;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -136,6 +137,10 @@ static NSString* const kDefaultLoadedTimeKey = @"RKRequestTTModelDefaultLoadedTi
|
||||
|
||||
- (BOOL)isOutdated {
|
||||
NSTimeInterval sinceNow = [self.loadedTime timeIntervalSinceNow];
|
||||
if (![self isLoading] && !_emptyReloadAttempted && _objects && [_objects count] == 0) {
|
||||
_emptyReloadAttempted = YES;
|
||||
return YES;
|
||||
}
|
||||
return (![self isLoading] && (-sinceNow > _refreshRate));
|
||||
}
|
||||
|
||||
@@ -239,13 +244,11 @@ static NSString* const kDefaultLoadedTimeKey = @"RKRequestTTModelDefaultLoadedTi
|
||||
- (void)load {
|
||||
RKManagedObjectStore* store = [RKObjectManager sharedManager].objectStore;
|
||||
NSArray* cacheFetchRequests = nil;
|
||||
NSArray* cachedObjects = nil;
|
||||
if (store.managedObjectCache) {
|
||||
cacheFetchRequests = [store.managedObjectCache fetchRequestsForResourcePath:self.resourcePath];
|
||||
cachedObjects = [RKManagedObject objectsWithFetchRequests:cacheFetchRequests];
|
||||
}
|
||||
|
||||
if (!store.managedObjectCache || !cacheFetchRequests || _cacheLoaded || [cachedObjects count] == 0) {
|
||||
if (!store.managedObjectCache || !cacheFetchRequests || _cacheLoaded) {
|
||||
RKObjectLoader* objectLoader = [[RKObjectManager sharedManager] objectLoaderWithResourcePath:_resourcePath delegate:self];
|
||||
objectLoader.method = self.method;
|
||||
objectLoader.objectClass = _objectClass;
|
||||
@@ -257,7 +260,7 @@ static NSString* const kDefaultLoadedTimeKey = @"RKRequestTTModelDefaultLoadedTi
|
||||
[objectLoader send];
|
||||
} else if (cacheFetchRequests && !_cacheLoaded) {
|
||||
_cacheLoaded = YES;
|
||||
[self modelsDidLoad:cachedObjects];
|
||||
[self modelsDidLoad:[RKManagedObject objectsWithFetchRequests:cacheFetchRequests]];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user