4

I am building an app in which I am implementing offline mode as well. For this I used NSURLCache mechanism. When the app is in foreground then the cache mechanism works perfectly even if the device goes in offline mode. The problem comes when I quit the app and then open it in offline mode. This time NSCachedURLResponse is returning nil for the particular request.

Below is the code I am using for this:

Custom Cache Creation:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024 diskCapacity:1024*1024*5 diskPath:nil];
    [NSURLCache setSharedURLCache:urlCache];
}

Calling server and cached object:

-(void)serverCall
{
   NSURLCache *urlCache = [NSURLCache sharedURLCache];

   NSString *urlString = nil;
   NSHTTPURLResponse *response = nil;
   NSError *error = nil;

   urlString = [NSString stringWithFormat:@"%@%@",BASE_URL,url];
   urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

   NSURL *finalUrl = [NSURL URLWithString:urlString];
   NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
   request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

   NSData *data = nil;

   if ([self checkInternetConnection])
   {
       data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

       NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
       [urlCache storeCachedResponse:cachedResponse forRequest:request];
   }
   else
   {
       NSCachedURLResponse *cachedResponse = [urlCache cachedResponseForRequest:request];
       response = cachedResponse.response;
       data = cachedResponse.data;
   }
}

I can see that caches.db in library path of application the response is saved. But when i am trying to read it in Offline mode (after app is killed from background), the cached response is coming nil. I have gone through following links (and many more) but couldn't find the solution of my problem.

NSURLCache Problem with cache response

How to use NSURLCache to return cached API responses when offline (iOS App)

http://twobitlabs.com/2012/01/ios-ipad-iphone-nsurlcache-uiwebview-memory-utilization/

http://petersteinberger.com/blog/2012/nsurlcache-uses-a-disk-cache-as-of-ios5/

Bypassing http response header Cache-Control: how to set cache expiration?

I have checked the http header fields and cached header in the api response and the problem is not from server end.

I have also tried providing a path in diskPath param while creating the custom cache but then it doesn't even load the cached object while the app is in foreground and internet disconnects. I have also changed the expiration date but the working is still same.

I have tried using SDURLCache but I am facing the similar problem. However I have successfully achieved this with ASIHTTPRequest but I don't want to use it as I have written all the server operations once already and I have to change every request with ASIHTTPRequest method.

Please help me find a solution for this. Thanks in advance....

Community
  • 1
  • 1
minti
  • 41
  • 3
  • Are you able to fix this problem? I am also giving same issue. Data is there in cache.db but its not loading and if you reload app multiple times it will sometimes load data. That's weird. – Arsalan Sep 21 '16 at 06:14
  • @Arsalan No, I wasn't able to find an solution. At that time I used ASIHTTPRequest to achieve this. Thought I am still facing the same issue while using NSURLCachePolicy. – minti Sep 22 '16 at 07:35

0 Answers0