Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
Last active December 31, 2015 18:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicklockwood/8025593 to your computer and use it in GitHub Desktop.
Save nicklockwood/8025593 to your computer and use it in GitHub Desktop.
NSCache seems to be broken in iOS 7 - it doesn't empty itself, even if your app runs out of memory. This is a simple category to fix the problem.
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation NSCache (Fix)
+ (void)load
{
//swizzle init
Method a = class_getInstanceMethod(self, @selector(init));
Method b = class_getInstanceMethod(self, @selector(init_NSCF));
method_exchangeImplementations(a, b);
//swizzle dealloc - yeah, I went there
a = class_getInstanceMethod(self, NSSelectorFromString(@"dealloc"));
b = class_getInstanceMethod(self, @selector(dealloc_NSCF));
method_exchangeImplementations(a, b);
}
- (id)init_NSCF
{
if ((self = [self init_NSCF]))
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}
- (void)dealloc_NSCF
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self dealloc_NSCF];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment