在 applicationWillResignActive 中发送内存警告

发布于 2024-12-08 19:01:53 字数 634 浏览 0 评论 0原文

几天前,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

貪欢 2024-12-15 19:01:53

我在我的应用程序 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.

尸血腥色 2024-12-15 19:01:53

这也许是可能,但是形式有点糟糕。

您可以轻松编写自己的通知。只需打开项目的预编译头文件(通常在“其他源”或“支持文件”下,并确保它包含此 #define。PCH

#ifdef __OBJC__
    #define MyApplicationDidReceiveMemoryWarning @"myMemoryWarningString"
#endif

设置 NSTimer

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(sendMemoryNotification) userInfo:nil repeats:YES];

有效地包含在您编译的每个文件中。在 applicationDidFInishLaunch:withOptions:中 sendMemoryNotification 方法,只需发布​​通知

-(void)sendMemoryNotification
{
    [[NSNotificationCenter defaultCenter] postNotification:MyApplicationDidReceiveMemoryWarning];
}

任何持有重要数据的类都会注册为 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.

#ifdef __OBJC__
    #define MyApplicationDidReceiveMemoryWarning @"myMemoryWarningString"
#endif

THe PCH is effectively included in every file you compile. Set up an NSTimer in applicationDidFInishLaunch:withOptions:

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(sendMemoryNotification) userInfo:nil repeats:YES];

in the sendMemoryNotification method, just post the notification

-(void)sendMemoryNotification
{
    [[NSNotificationCenter defaultCenter] postNotification:MyApplicationDidReceiveMemoryWarning];
}

Any class that holds significant data register as an observer for the key in the NSNotificationCenter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文