0

I am very confused by iOS caching system. It should be straightforward but it not (for me). I am writing an database App which allows for external web access to a limited range of additional reference material in the form of web pages. Sometimes a user might need access to the external web pages while in the field where there is no WiFi or Cell data. Simple, I thought, use URLCaching while iterating through the set of external pages during a time when the internet is available. Then, use a request policy of NSURLRequestReturnCacheDataElseLoad. So, I created a shared cache in the AppDelegate, then a singleton session:

    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.URLCache = self->_myCache;
    sessionConfig.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;
    sessionConfig.HTTPMaximumConnectionsPerHost = 5;
    session = [NSURLSession sessionWithConfiguration:sessionConfig
                                            delegate:self
                                       delegateQueue:nil];

Then fill the cache using dataTask (since a trial of downloadTask did not fill cache):

    NSURLSessionDataTask *myTask = [[NSURLSessionDataTask alloc] init];
do {
      myTask = [self.session dataTaskWithRequest:req];
      myTask.taskDescription = searchName; //Iterating several values of searchName
      [myTask resume]; } while ...searchNames...

This fills the cache as evidenced by cache.currentDiskUsage and cache.currentMemoryUsage. Run the web read loop after initial fetches does not further increase size of cache.

Here is the problem: Running this code fragment with internet off fails to read from cache

NSURL *nsurl=[NSURL URLWithString:@"https:searchName"];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
[_webView loadRequest:nsrequest];

in addition, once cache is filled, trying to repeat the initial round of fetches with internet off, leads to

TIC TCP Conn Failed [1:0x6000028ce580]: 1:50 Err(50)

Is there no cache interoperability between HTTP if filled with dataTask and reading back from cache with NSURLRequest? What am I doing wrong? Is there a different approach to solve my design goal?

BlueskyMed
  • 765
  • 7
  • 24

1 Answers1

0

After three days of experimentation, I am able to create a cache of about three hundred web pages (with associated embedded images) and read them back from persistent memory, across launches of the app. The only approach to accessing the cache is to use the NSURLCache cachedResponseForRequest: method. Using other forms of accessing a URL with appropriate CachePolicy do not seem to work.

There does seem to be an unfortunate side effect of the cachedResponseForRequest: method in that it only passes the original html document to wkwebview, and none of the linked resources which are verified to also be in my cache.

Does anyone have a suggestion to get this limit?

BlueskyMed
  • 765
  • 7
  • 24