Question

This post recommends doing this:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

However, that will take both the on disk and the in memory cache. Does something like this work to just evict the in memory cache?

NSURLCache *cache = [NSURLCache sharedURLCache];
NSUInteger currentMemoryCapacity = cache.memoryCapacity;

// Not sure if this works? Docs seem to say it will trigger an evict.
// https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLCache/setMemoryCapacity:
cache.memoryCapacity = 0; 
cache.memoryCapacity = currentMemoryCapacity;
Was it helpful?

Solution

Yes, that code should evict the memory cache. (And does on iOS 7.1.1, in my brief test.)

If you're looking for finer control over the cache, check out Peter Steinberger's fork of SDURLCache which has (for example) a -removeAllCachedResponsesInMemory method.

OTHER TIPS

This no longer appears to work (tested on the iOS 9.2 simulator)

Although the cache's reported memoryCapacity changes, the actual memoryUsage reported doesn't :-(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top