博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
didRecevieMemoryWarning 和 ViewDidUnload
阅读量:4200 次
发布时间:2019-05-26

本文共 3178 字,大约阅读时间需要 10 分钟。

  • didReceiveMemoryWarning practices

As you said, the controller's default implementation of didReceiveMemoryWarning releases its view if it is 'safe to do so'. While it's not clear from Apple's documents what 'safe to do so' means, it is generally recognized as it has no superview (thus there is no way that the view is currently visible), and itsloadView method can rebuild the entire view without problems.

The best practice when you override didReceiveMemoryWarning is, not to try releasing any view objects at all. Just release your custom data, if it is no longer necessary. Regarding views, just let the superclass's implementation deal with them.

Sometimes, however, the necessity of the data may depend on the state of your view. In most cases, those custom data is set in viewDidLoad method. In these cases, 'safe to release custom data' means that you know that loadView and viewDidLoad will be invoked before the view controller uses the custom data again.

Therefore, in your didReceiveMemoryWarning, call the superclass implementation first, and if its view is unloaded, then release the custom data because you know that loadView and viewDidLoad will be invoked again for sure. For example,

- (void)didReceiveMemoryWarning {
    /* This is the view controller's method */    [super didReceiveMemoryWarning];    if (![self isViewLoaded]) {
        /* release your custom data which will be rebuilt in loadView or viewDidLoad */    }}

Be careful not to use self.view == nil, because self.view assumes that the view is needed for someone and will immediately load the view again.

  • viewDidUnload method

viewDidUnload is called when the view controller unloaded the view due to a memory warning. For example, if you remove the view from the superview and set the view property of the controller tonilviewDidUnload method will not be invoked. A subtle point is that even if the view of a view controller is already released and set to nil by the time the controller receivesdidReceiveMemoryWarning, so actually there is no view to unload for the controller,viewDidUnload will be invoked if you call the superclass's implementation ofdidReceiveMemoryWarning.

That's why it's not a good practice to manually set the view property of a view controller to nil. If you do, you may better send a viewDidUnload message as well. I guess your understanding ofviewDidUnload is more desirable, but apparently it's not the current behavior.

  • Popping view controllers

If you mean 'removing from the superview' by 'popping', it does decrease the retain count of the view, but not necessarily deallocate it.

If you mean popping out from a UINavigationController, it actually decrease the retain count of the view controller itself. If the view controller is not retained by another object, it will be deallocated, desirably with its view. As I explained, viewDidUnload will not be invoked this time.

  • Others...

Technically, the retain count may not go down to zero. The object is more likely to be just deallocated without setting the count to zero beforehand.

Just to make sure, the view controller itself is normally not deallocated by default behaviors due to the memory warning.

转载地址:http://tzbli.baihongyu.com/

你可能感兴趣的文章
Redis和Memcache对比及选择
查看>>
用谷歌账号登陆magento、
查看>>
php oauth 模块在linux下安装
查看>>
代替nginx的服务器 - The Tengine Web Server
查看>>
nginx 升级成 tengine 的代码
查看>>
magento rest api 调用
查看>>
magento rest api 调用!
查看>>
magento rest api get Token key and secret
查看>>
谷歌获取货币汇率代码
查看>>
安装nginx
查看>>
手动6 - 隐藏Nginx版本号
查看>>
手动7 - nginx 日志切割
查看>>
magento - 使用后台设置的时间用法
查看>>
Linux常用命令
查看>>
清除文件中的.svn文件
查看>>
手动11 -nginx 优化配置
查看>>
php加速器 - zendopcache
查看>>
手动12 - 安装php加速器 Zend OPcache
查看>>
set theme -yii2
查看>>
yii2 - 模块(modules)的view 映射到theme里面
查看>>