在 applicationWillResignActive 中发送内存警告
几天前,我在 SO 上发现了一个很好的片段,通过每隔 10 秒左右发送一个 UIApplicationDidReceiveMemoryWarningNotification
通知来测试内存警告。
我真的很喜欢这个想法,并采用它只在应用程序切换到后台时发送通知,因为它可以更轻松地清理一些内存,因为每个 UIViewController
无论如何都会监听此通知(并执行我也从中受益的一些内存清理)。
由于我是一个懒惰的程序员,我想知道苹果是否允许这种技巧。 从技术上讲,这不是私有 API 使用,我什至怀疑他们在测试过程中是否发现了这样的东西,但是,我对此并不能 100% 确定。我正在使用的代码如下所示:
- (void)applicationWillResignActive:(UIApplication *)application
{
// Do some other stuff, unrelated to the question
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:application];
}
A few days ago I found a nice snippet on SO to test memory warnings by just sending a UIApplicationDidReceiveMemoryWarningNotification
notification every 10 seconds or so.
I really liked the idea and adopted it to only send out the notification when the app switches into the background, since it makes it easier to clean up some memory since every UIViewController
listens to this notification anyway (and performs some memory cleaning that I also benefit from).
Since I'm a lazy programmer I wondered if this kind of trick is allowed by Apple or not.
Technically this not an private API usage and I even doubt that they find such things during testing, however, I'm not 100% sure about this. The code that I'm using looks like this:
- (void)applicationWillResignActive:(UIApplication *)application
{
// Do some other stuff, unrelated to the question
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification object:application];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我的应用程序 iFerien Deluxe 中做了同样的事情,它通过了审批流程,没有任何问题。
就我个人而言,我不认为这对苹果来说是一个问题。
但在我当前的应用程序中,我试图避免这种情况,因为它看起来有点老套,因为你最好在 applicationWillResignActive 中手动释放内存。
I had done the same thing in my App iFerien Deluxe and it went through the approval process without any problems.
Personally I don't think, that this is a problem for Apple.
But in my current app I'm trying to avoid this, because it looks a bit hacky, as you better had to manually free your memory in applicationWillResignActive.
这也许是可能,但是形式有点糟糕。
您可以轻松编写自己的通知。只需打开项目的预编译头文件(通常在“其他源”或“支持文件”下,并确保它包含此 #define。PCH
设置 NSTimer
有效地包含在您编译的每个文件中。在 applicationDidFInishLaunch:withOptions:中 sendMemoryNotification 方法,只需发布通知
任何持有重要数据的类都会注册为 NSNotificationCenter 中键的观察者。
It may be possible, but it's a little bad form.
You could easily write your own notification. Just open your project's precompiled header file (usually under "Other Sources" or "Supporting Files" and make sure it includes this #define.
THe PCH is effectively included in every file you compile. Set up an NSTimer in applicationDidFInishLaunch:withOptions:
in the sendMemoryNotification method, just post the notification
Any class that holds significant data register as an observer for the key in the NSNotificationCenter.