NSCache
does not respond to UIApplicationDidReceiveMemoryWarningNotification
, but it automatically UIApplicationDidReceiveMemoryWarningNotification
its objects in low-memory situations, obviously using some other mechanism.
While I previously suggested watching UIApplicationDidReceiveMemoryWarningNotification
, this is not the case. No special handling is required for low memory situations, as NSCache
handles this automatically.
Update:
As in iOS 7, NSCache
not only does not respond to memory warnings, but also does not seem to properly clean itself of memory pressure (see NSCache crashes when the memory limit is reached (only on iOS 7) ).
I subclass NSCache
observe UIApplicationDidReceiveMemoryWarningNotification
and clear the cache when warning the memory:
@interface AutoPurgeCache : NSCache @end @implementation AutoPurgeCache - (id)init { self = [super init]; if (self) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; } return self; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
source share