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;
有帮助吗?

解决方案

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.

其他提示

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 :-(

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top