0

I'm trying to get my app to use the Last-Modified headers provided by my server.

Problem is that the app keeps caching responses from the server, and I have tried again and again to clear the caches, not allow it to cache responses, etc. but NOTHING is working. I have tried the below:

for AFNetworking:

- (AFHTTPRequestOperationManager *)manager {
    if (!_manager) {
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [_manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    }
    return _manager;
}

Setting the NSURLRequestReloadIgnoringLocalCacheData didn't work.

I tried this in my delegate:

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                     diskCapacity:20 * 1024 * 1024
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

I even tried to set them to 0 to eliminate all possibility of caching. Still didn't work.

I also tried to delete cache as per below:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] removeCachedResponsesSinceDate:last];

Even that didn't work. When I get a new response from the server, it simply reloads from cache yet again! Is there anything else I haven't tried that would help here? When I remove the Last-Modified header for my servers response, everything works. But this is not the correct solution.

I have also read the below:

http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/

AFNetworking - do not cache response

Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162
  • I remember adding an extra unique param to url (like timestamp for example) this forcing cache to be reloaded.Not sure if it is the case like webpages cached and not reloaded even if they contain js to reload. So basically we access the same url but adding a &timestamp=572888166777(something like that) – ares777 Mar 05 '15 at 07:36
  • It might be server side issue? please try once @user3344236 solution. –  Mar 05 '15 at 07:39
  • I don't think its a server side issue -- works perfectly for my Android app. Also, I use Django which automates most of it. – KVISH Mar 05 '15 at 07:39
  • You probably have to use NSURLRequestReturnCacheDataElseLoad mode. – gabbler Mar 05 '15 at 07:43
  • how will `NSURLRequestReturnCacheDataElseLoad` not use the cache? I'm trying to prevent it from loading from cache. – KVISH Mar 05 '15 at 07:45
  • Sorry, misunderstood your question. – gabbler Mar 05 '15 at 07:59
  • it should work with all of these. Can you clean build your project and redeploy to device. As mentioned before, you can add some unique keys to end of url. That solves either server side cache and local cache – kocakmstf Mar 05 '15 at 08:34
  • How would unique keys work? – KVISH Mar 05 '15 at 14:29

1 Answers1

0

I suggest try adding this modyfied method - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

as:

     - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
    {
    if (self.cacheResponse) {
    //we comment this line and add  return nil  return self.cacheResponse(connection, cachedResponse);
    return nil;     
    } else {
    if ([self isCancelled]) {
    return nil;
    }
    return cachedResponse;
    }
    }

Another hack should work:

  [requestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
return nil;
 }];

where requestOperation is your network request opperation.

More inspired from SDWebimage

  - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
   responseFromCached = NO; // If this method is called, it means the response wasn't read from cache
   if (self.request.cachePolicy ==  NSURLRequestReloadIgnoringLocalCacheData) {
    // Prevents caching of responses
    return nil;
    }
    else {
    //we modify also this to return nil return cachedResponse;
    return nil;    
   }
 }
ares777
  • 3,590
  • 1
  • 22
  • 23