RestKit/RestKit — Issue #552: RKRequestCachePolicyFromCacheAndNetwork
Author: grgcombs
Created: 2012-02-09 03:07:19+00:00
State: open

@jeffarena and @blakewatters :

I'm noodling around RestKit on behalf of a secret project and part of it deals with some modifications to the request cache policy. I'm wondering if these mods are worth a pull request or not... do you think this has much general appeal for other projects beyond mine? If so, I can put together a pull request with some unit tests to match.

I'm proposing adding an additional policy to the request cache. For each cacheable request to a single endpoint, it will return a response from the cache if available and then if the network is reachable it will also fire off a request to the network. So, if a response is available from the cache *and* from the network, you'll get two `didFinishLoad:` calls for the price of one `get:`.

```diff
-------------------------- Code/Network/RKRequest.h ---------------------------
index 2c434ee..34d5977 100644
@@ -62,10 +62,12 @@ typedef enum {
// Load from the cache if we have data stored
RKRequestCachePolicyEnabled = 1 << 3,

// Load from the cache if we are within the timeout window
RKRequestCachePolicyTimeout = 1 << 4,
+
+ RKRequestCachePolicyFromCacheAndNetwork = 1 << 5 | RKRequestCachePolicyEnabled,

RKRequestCachePolicyDefault = RKRequestCachePolicyEtag | RKRequestCachePolicyTimeout
} RKRequestCachePolicy;

/**

--------------------------- Code/Network/RKRequest.m ---------------------------
index 54f1ed2..43b6a24 100644
@@ -468,10 +468,12 @@ RKRequestMethod RKRequestMethodTypeFromName(NSString *methodName) {
_sentSynchronously = NO;
if ([self shouldLoadFromCache]) {
RKResponse* response = [self loadResponseFromCache];
_isLoading = YES;
[self performSelector:@selector(didFinishLoad:) withObject:response afterDelay:0];
+ if ( ([self shouldDispatchRequest]) && (self.cachePolicy & RKRequestCachePolicyFromCacheAndNetwork) )
+ [self fireAsynchronousRequest];
} else if ([self shouldDispatchRequest]) {
#if TARGET_OS_IPHONE
// Background Request Policy support
UIApplication* app = [UIApplication sharedApplication];
if (self.backgroundPolicy == RKRequestBackgroundPolicyNone ||

```