0
votes

I've used AFNetworking to get http response from remote server. And remote server will return 304 code when the header of http request contains "if-none-match" and "cache-control" fields.

Based on NSURLCache class reference, it has provided request cache. But how to integrate AFNetworking and NSURLCache with remote server in order to cache the http response based on Cache-Control section of RFC 2616

P.S : I found AFNetworking has cached the response quoted from https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ . So the cache problem is solved. However, I've had another problem when the param of request always has some minor difference(i.e signature) which can not influence the response from remote server. So I wonder to know whether NSURLCache can cache NSURLRequest ignoring its param.

For example, it has cached the response of "http://google.com/test?param1=test&sign=1". When i send "http://google.com/test?param1=test&sign=2" to remote server, the remote server return the same response which i want to cache.

1

1 Answers

0
votes

Yes, sort of.

  • Create a method that strips the offending bits from a request and returns the modified request.
  • Implement URLSession:dataTask:willCacheResponse:completionHandler: delegate method (or the equivalent NSURLConnection delegate method if you're using that API) in your session delegate.
  • In that method, call the strip method, cache the item yourself, and call the completion handler with a nil response.
  • Before you make a request, call the strip method and check to see if that item is in the cache. Check the response yourself to see if it is still valid. If so, return it immediately. Otherwise, make the request.

Unfortunately, I don't think there's a way to do it that is more automated than that.