Thursday, August 14, 2014

Behavior of ios cache on NSURLConnection and UIWebView

iOS provides NSUrlCache for application to manage cache behavior, but this class is more for native connection with NSUrlConnection and NSURLSession, if the application involves UIWebView request, then using NSURLCache may not get expected behavior. As the cache behavior in UIWebView does not go through the NSUrlCache API.

To verify this, a simple testing app is created to send both xmlhttprequest in UIWebView and native NSURLConnection request, it also subclass the default NSUrlCache to get more log and control for the testing. The web server will return a response with Cache-control header of "max-age=1000", so as to be sure the cache is valid.

1. NSURLConnection request:
When sending native NSURLConnection request, response is cached for the first request, in the following requests, [NSURLCache cachedResponseForRequest] is always called to return the cached response, and the request does not go to server as expected.

If [NSURLCache RevmoeAllCachedRespones] is called, the cache will be cleared, and the next request will go to server to get response

If [NSURLCache StoreCacheResponse] is disabled to never store response, then each request will go to server, as no cache is available on client side.

2. WebView xmlhttprequest
When sending xmlhttprequest from uiWebView, response is cached for the first request, however, for the following request, the cached response will be loaded automatically without invoking [NSUrlCacah cachedResponseForRequest] method, basically, UIWebView handles the cache by itself without NSUrlCache.

If  [NSURLCache RevmoeAllCachedRespones] is called, when sending the xmlhttprequest, it can still get the cached response from somewhere without going to server.

If [NSURLCache StoreCacheResponse] is disabled to never store response, it does not affect the xmlhttpRequest for uiWebView, it can still get cached response with going to server

3. NSUrlProtocol
This behavior also affects NSUrlProtocol if it is used to cached request from UIWebView, if a xmlhttpRequest from uiWebView is already cached, then the uiWebView will directly load the cached response without invoking the canInitWithRequest method, so the NSUrlProtcol does not even get a chance to handle the request.

In addition, for requests sent from uiWebView xmlhttprequest, even if willCacheResponse method to not allow NSUrlProtocol's NSUrlConnection to store any cache, the next request can still get cached response directly without canInitWithRequest be called. Also, calling [NSURLCache RevmoeAllCachedRespones] has no effect on the cached data by xmlhttprequest, it can still load cached response successfully.

Based on the above testing, the NSURLCache API only works as expected for native NSUrlConnection function. It does not work for xmlhttpRequest sent from UIWebView.



1 comment:

  1. I love it here. Keep sharing your good vibes. I love them Are you also searching for assignment help UK? we are the best solution for you. We are best known for delivering cheap assignments to students without having to break the bank

    ReplyDelete