문제

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