如何实现 didReceiveMemoryWarning? [英] How to implement didReceiveMemoryWarning?

查看:16
本文介绍了如何实现 didReceiveMemoryWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的位置感知 iPhone 应用程序,除了手机内存不足的情况外,它在功能上运行良好,符合我们的预期.

I have developed a simple location aware iPhone application which is functionally working very well to our expectations except in the low memory condition of the phone .

在手机内存不足的情况下,我的应用程序会崩溃,如果我通过释放一些空间来增加手机内存,它会再次开始正常运行而不会发生任何崩溃.

In low memory condition of the phone my app just crashes and If I increases the phone memory by freeing up some space it again start working well without any crash .

当我对这个问题进行一些谷歌搜索时,我发现在内存不足的情况下,操作系统会将 didReceiveMemoryWarning 发送到当前层次结构中的所有控制器,以便它们中的每一个都应该实现 didReceiveMemoryWarning 方法,并将 iboutlet 设置为 nil当前不可见的视图.

when I did some googling on the problem I found that in the low memory conditions the OS will send didReceiveMemoryWarning to all the controllers in the current hierarchy so that each one of them should implement didReceiveMemoryWarning method and also set iboutlet to nil for the view that is currently not visible .

我还在某处读到,如果该控制器的视图不可见,则将调用带有 nil 参数的方法 setView 并且如果视图附加了一些出口变量,则删除时会出现问题

I have also read somewhere that if the view for that controller is not visible the method setView with nil parameter will be called and if there are some outlet variables attached to view there will be problem in removing them.

因此,在所有这些基础上,通过实施 didReceiveMemoryWarningviewDidUnload 方法来处理由 Iphone 引发的低级内存状况的最佳方法是什么.

So with all these fundas what is the best to handle low level memory condition raised by the Iphone by implementing the didReceiveMemoryWarning and viewDidUnload methods.

如果可能,请给出正确的例子或链接以解决上述问题.

Please give a proper example or link if possible for the solution of the above problem .

谢谢.

推荐答案

我发布的一个例子...我从某处复制的...它可能会给你一些想法...

One example i am posting...which i have copied from somwhere... it might give you some idea...

- (void)didReceiveMemoryWarning {

    // Release anything that's not essential, such as cached data (meaning
    // instance variables, and what else...?)

    // Obviously can't access local variables such as defined in method
    // loadView, so can't release them here We can set some instance variables
    // as nil, rather than call the release method on them, if we have defined
    // setters that retain nil and release their old values (such as through use
    // of @synthesize). This can be a better approach than using the release
    // method, because this prevents a variable from pointing to random remnant
    // data.  Note in contrast, that setting a variable directly (using "=" and
    // not using the setter), would result in a memory leak.
    self.myStringB = nil;
    self.myStringD = nil;
    [myStringA release];// No setter defined - must release it this way
    [myStringC release];// No setter defined - must release it this way

    /* 3. MUST CONFIRM: NOT necessary to release outlets here - See override of
       setView instead.
    self.labelA = nil;
    self.imageViewA = nil;
    self.subViewA = nil;
     */
    // Releases the view if it doesn't have a superview
    [super didReceiveMemoryWarning];
}

这篇关于如何实现 didReceiveMemoryWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆