在收到内存警告时清除共享的NSURLCache
是否是一个好的做法?就像这样:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
我可以假设这样做是否也会不必要地清除磁盘缓存?
如果是这样的话,是否可以只清除内存缓存?
在收到内存警告时清除共享的NSURLCache
是否是一个好的做法?就像这样:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
我可以假设这样做是否也会不必要地清除磁盘缓存?
如果是这样的话,是否可以只清除内存缓存?
当出现内存警告时,您只需要清除内存缓存而不是磁盘缓存。使用removeAllCachedResponses
的问题在于它会清除两者。根据我的测试,这似乎只清除了内存缓存。
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
NSURLCache * const urlCache = [NSURLCache sharedURLCache];
const NSUInteger memoryCapacity = urlCache.memoryCapacity;
urlCache.memoryCapacity = 0;
urlCache.memoryCapacity = memoryCapacity;
}
URLCache.shared.currentMemoryUsage
before and after your code gives the same value, so setting memoryCapacity
to 0
seems doesn't work. On the other hand, after URLCache.shared.removeAllCachedResponses()
, currentMemoryUsage
is set to 0
- rafalkitta