Last active
December 31, 2015 18:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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