[Ios6] Departure and replacement of viewDidUnload method

[Ios6] Departure and replacement of viewDidUnload method

This article is reprinted, the source is at the end, before copying, let me talk about my own summary:

The viewDidUnload method has been abandoned in ios6. Many people say that the alternative method is Memory warning. You can directly set some ui attributes to nil in this method. I found a lot of information. I don’t think so, so the summary is: the current situation , You don’t have to worry about where to set the UI attribute=nil, just release it in dealoc, you don’t need to consider the issue of nil, if you have to pursue the ultimate, then release it in dealoc and then=nil, it’s not impossible .

In iOS6, the viewDidUnload callback method is Deprecated. Check Apple's documentation, you can see the following instructions:

View Image

So, what should be done with the code originally in viewDidUnload? How to deal with memory warnings in iOS6? With these questions, I searched for some information, and I share it with you.


In iOS4 and iOS5, when the memory is insufficient and the application receives a Memory warning, the system will automatically call the viewDidUnload method of the ViewController that is not currently on the interface. Normally, these ViewControllers that are not displayed on the interface are the ViewControllers that are not on the top of the UINavigationController Push stack, and the child ViewControllers that are not displayed in the UITabBarViewController. These View Controllers will automatically call the viewDidUnload method by the system when the Memory Warning event occurs.


In iOS6, because the viewDidUnload event will not be triggered under any circumstances in iOS6, Apple suggests in the document that the related operations of reclaiming memory should be moved to another callback function: didReceiveMemoryWarning. But if you just move the code previously written in the viewDidUnload function to the didReceiveMemoryWarning function, then you are wrong. The following is an incorrect sample code:

1.- (void)didReceiveMemoryWarning { 2. [super didReceiveMemoryWarning]; 3. if([self isViewLoaded] && ![[self view] window]) { 4. [self setView:nil]; 5. } 6.}

This article explains why iOS6 does not recommend you to set the view to nil (the link needs to be turned over), the translation is as follows:

1. UIView has a CALayer member variable, CALayer is specifically used to draw itself on the screen. As shown below:

View Image

2. CALayer is a container class for bitmap images. When UIView calls its own drawRect, CALayer will create this bitmap image class.


3. The specific memory is actually a bitmap image class, CALayer only occupies 48bytes, UIView only occupies 96bytes. And the bitmap class of an iPad's full-screen UIView will occupy 12M!


4. In iOS6, when the system issues a MemoryWarning, the system will automatically recycle the bitmap class. But UIView and CALayer classes are not recycled. In this way, most of the memory is reclaimed, and when the bitmap class is needed, it can be rebuilt by calling the drawRect: method of UIView.


Memory optimization
In addition, the article also mentions a memory optimization technique that Apple's operating system has made for this, which is explained as follows:


When a piece of memory is allocated, it will be marked as "In use" to prevent it from being reused. When the memory is released, this memory will be marked as "Not in use", so that when there is a new memory application, this memory may be allocated to other variables.


The private member variable type of the specific bitmap content included in CALayer is CABackingStore . When a MemroyWarning is received, the memory area of ​​the CABackingStore type will be marked as volatile (here volatile is not the same as C and volatile in the Java language), and volatile means , This memory may be reused by the original variable again.


In this way, after the above optimization, when a Memoy Warning is received, although all the bitmap memory contained in the CALayer is marked as volatile, as long as this memory is not reused again, when the bitmap memory needs to be rebuilt , It can be reused directly without calling UIView's drawRect: method again.


So, in simple terms, for iOS6, you don't need to do any previous viewDidUnload things, let alone move the previous viewDidUnload code to the didReceiveMemoryWarning method.

Quoting a passage from WWDC 2012 to say goodbye to viewDidUnload:

The method viewWillUnload and viewDidUnload. We're not going to call them anymore. I mean, there's kind of a cost-benifit equation and analysis that we went through. In the early days, there was a real performance need for us to ensure that on memory warnings we unloaded views. There was all kinds of graphics and backing stores and so forth that would also get unloaded. We now unload those independently of the view, so it isn't that big of a deal for us for those to be unloaded, and there were so many bugs where there would be pointers into.

 Reference link

Source: http://blog.devtang.com/blog/2013/05/18/goodbye-viewdidunload/

Reference : https://blog.csdn.net/mad1989/article/details/9001308