Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

NSURLCacheStoragePolicy #125

wants to merge 46 commits into


None yet
2 participants

I added support for changing the NSURLCacheStoragePolicy.

You only need to cherry pick seatme/AFNetworking@e2e14cc. Hopefully that should apply cleanly. Our versions are growing a little divergent as I build on our changes but this one feature may apply cleanly upstream so I wanted to make note of it before I continue to move forward.

zbowling and others added some commits Sep 22, 2011

@zbowling zbowling async fixes. changes for json d3af887
@zbowling zbowling I fixed something e1d89f8
@zbowling zbowling small change' dd84bad
@zbowling zbowling better foundation json support ef5bf73
@zbowling zbowling Merge branch 'merge_foundation_json' of github.com:seatme/AFNetworking
	Mac Example/AppDelegate.h
	Mac Example/AppDelegate.m
	Mac Example/Classes/AFGowallaAPIClient.h
	iOS Example/Classes/AFGowallaAPIClient.h
@zbowling zbowling brought to much over in the merge d55245e
@zbowling zbowling better JSON support 2c0ef65
@zbowling zbowling better idea e42d89d
@zbowling zbowling class custer method 23ac76c
@zbowling zbowling should be self d6ffe69
@zbowling zbowling much much better ec9a8b0
@zbowling zbowling moved this to the same file 54f90d4
@zbowling zbowling moved this to the same file b3cf84e
@zbowling zbowling merged in upstream changes 6c1cd78
@zbowling zbowling Merge branch 'experimental-0.8' of https://github.com/gowalla/AFNetwo…

@zbowling zbowling fixed a lot of various changes after the merge. much better bb96088
@zbowling zbowling forgot to mark this as concurrent. a3c3322
@zbowling zbowling self is never delegate. don't unset the delegate if someone else want…
…ed to be the delegate.
@zbowling zbowling no such thing as an error here. 1cfe657
@zbowling zbowling comments. 70fd2d8
@zbowling zbowling dead property 9834ad4
@zbowling zbowling comments 932d710
@zbowling zbowling Foundation json should be last. 2a939e5
@zbowling zbowling conventions and leaks 9a6d541
@zbowling zbowling Merge branch 'experimental-0.8' of https://github.com/gowalla/AFNetwo…

@zbowling zbowling Merge branch 'experimental-0.8' of https://github.com/gowalla/AFNetwo…

@zbowling zbowling fix path of this. a9e645d
@zbowling zbowling Pull this change in from other branch daaba29
@zbowling zbowling Merge github.com:seatme/AFNetworking b4335c0
@mattt mattt Updating README to point to new AFNetworking URL (https://github.com/… 920b01d
@zbowling zbowling Fix memory leak ccfcd43
@zbowling zbowling Merge branch 'master' of github.com:zbowling/AFNetworking e86aa7a
@zbowling zbowling Merge branch 'master' of git://github.com/gowalla/AFNetworking
@zbowling zbowling much better solution 9088768
@zbowling zbowling change upstream and merge 11057cf
@zbowling zbowling fix a few lingering issues after the merge b3fb0bb
@zbowling zbowling new README 695ea9c
@zbowling zbowling New README for specifying the changes in our fork. 8dd9494
@zbowling zbowling Update README.md e746bf4
@zbowling zbowling Remove silly, overly paranoid code in setCompletionBlock and let the …
…parent have a say in the isReady process so it can check dependent NSOperations.
@zbowling zbowling add support for iOS multitasking. 16b5839
@zbowling zbowling Merge branch 'master' of github.com:seatme/AFNetworking f1cb7aa
@zbowling zbowling Update README.md 87feccd
@zbowling zbowling move multitasking support up to AFHTTPURLRequest d56f873
@zbowling zbowling Merge branch 'master' of github.com:seatme/AFNetworking 552e0cf
@zbowling zbowling add cache policy support e2e14cc

mattt commented Nov 26, 2011

As I understand it, HTTP facilitates contracts between client and server, which are communicated through headers. As per the specifications for the Cache-Control header:

The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain.

To override this, then, is to violate that contract, which I can only imagine leading to unexpected behavior somewhere down the line. While the architecture of AFURLConnectionOperation make it straightforward to manually define this behavior (by overriding the delegate method in a subclass), I do not think that it's especially useful to better enable behavior that violates HTTP expectations.

AFNetworking already supports NSURLCacheStoragePolicy by correctly using NSURLConnection. Any server responses that instruct to cache itself on the client will already do so, into NSURLCache.

NSURLRequestCachePolicy is different from NSURLCacheStoragePolicy. NSURLRequestCache policy works on the NSURLConnection and NSURLCacheStoragePolicy works on the NSURLCache object.

NSURLRequestCachePolicy controls a few headers like Cache-Control. NSURLCacheStoragePolicy controls how you want to persist the response if the NSURLConnection thinks the response can be cached at all.

How you handle the Cache-Control header is entirely different from how you persist the response to locally. The Cache-Control header documentation refers to everyone along the chain (like proxy servers and reverse caches) but it has nothing to do with me as the client and how I cache the final response. That is entirely different.

If the response says I can cache, there is no expectation in HTTP that I will or that I will use any kind of real persistence when I do.

That is why there are two options in NSURLConnection. One controls how you send the Cache-Policy header, and the other handles persistance of the response. By default this is always NSURLCacheStorageAllowed.

The issue is that NSURLCache on iOS is extremely limited anyways. By default iOS only does in memory caching even though NSCachedURLResponse tells NSURLCache to use NSURLCacheStorageAllowed and not NSURLCacheStorageAllowedInMemoryOnly. By default iOS doesn't actually persist much of anything.

However, the great thing is with NSURLCache you can register a different shared NSURLCache, like SDURLCache or our soon to be released implementation, that will cache to disk in a very optimal pattern (almost like NSURLCache does on OS X).

They do this to save people from filling up space or generally wearing down the flash memory. If you are careful though there is nothing wrong with disk based caching and may improve your app's experience doing it depending on the kind of app you have. In our case we have a lot of images and want them to be cached.

Having this option allows you to finer grained control on the Mac and on iOS (if you are using a NSURLCache subclass) to keep the existing behavior or persist it longer or to just not persist at all.

@mattt mattt closed this Feb 21, 2012

Why was this closed? Don't understand. Not that it maters much but curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment