我的应用程序使用大量内存。通常它运行良好,但在一段时间未重新启动的已加载设备上,它将因臭名昭著的内存不足错误而被抛弃。
我想响应didReceiveMemoryWarning
并释放我的一些缓存。
但我的问题是我的应用程序基于 OpenGL ES 模板并且没有视图控制器。它只有一个 App Delegate,它包含对 glView 的引用。
我可以做些什么来捕获didReceiveMemoryWarning
消息以便我可以响应?
我的应用程序使用大量内存。通常它运行良好,但在一段时间未重新启动的已加载设备上,它将因臭名昭著的内存不足错误而被抛弃。
我想响应didReceiveMemoryWarning
并释放我的一些缓存。
但我的问题是我的应用程序基于 OpenGL ES 模板并且没有视图控制器。它只有一个 App Delegate,它包含对 glView 的引用。
我可以做些什么来捕获didReceiveMemoryWarning
消息以便我可以响应?
您还可以在任何您想要的类中将方法作为观察者添加到UIApplicationDidReceiveMemoryWarningNotification
通知中。代码可能像这样:
- (void) cleanMemory: (NSNotification*) notification {
// Save memory!
}
- (id) init { // Or any other function called early on.
// other init code
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(cleanMemory:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
return self;
}
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"Received memory warning!");
}