So what's the deal with ARC and releasing properties / subviews on viewDidUnload

I am still studying iOS development and working with various tutorials and books. Some preliminary ARC, some with ARC.

In some cases, we are taught to release all the properties and routines of ViewController on viewDidUnload, but in some cases they told me that this is no longer required.

Can someone give a definitive answer? In iOS 5+, you need to do everything:

-(void)viewDidUnload { [super viewDidUnload]; self.photoViewCell = nil; self.photoImageView = nil; self.firstNameTextField = nil; self.lastNameTextField = nil; } 

... or not? If so, is it only for properties that are descendants of the UIView, or is it for all ViewController properties?

thanks

+1
source share
3 answers

Thus, in each view there are several owners. When this owner counter (commonly called saveCount) reaches 0, this object is destroyed.

In iOS 5, we now have weak links, which essentially mean "don't own this object."

Before iOS 5, you will see in our header files

 IBOutlet UILabel *myLabel; 

And this shortcut has been added to the XIB file view. In this case, myLabel has 2 owners: it is a supervisor (view in the XIB file) and a view controller (using IBOutlet). When viewDidUnload is called, the view controller view was released, and therefore its ownership of myLabel disappeared. So, myLabel at the moment has only 1 owner, a view controller. Therefore, we needed to release it in viewDidLoad to make sure that it had no owners, and therefore it was destroyed.

In iOS 5, you'll often see this instead

 __weak IBOutlet UILabel *myLabel 

This suggests that we do not want the view controller to own myLabel. Thus, the sole owner is the view controller view. Therefore, when viewDidUnload is called, the view controller view is already released, and therefore its ownership of myLabel has also been released. In this case, myLabel now has no owners, and its memory is freed. No need for self.myLabel = nil; there is.

So, with iOS 5, it is recommended to make all your IBOutlets a weak link. In this case, you do not even need to implement viewDidUnload, since all the memory took care of you.

But even if you use iOS 5, if your IBOutlets are not weak links, you will need this code in viewDidUnload.

+8
source

viewDidUnload has nothing to do with saving an account, automatically or otherwise. This method will be called when the view is unloaded due to memory pressure, which means that you must also use the null (and free, not ARC) view elements that you reference. Failure to do so may mean that your application does not free up enough memory when under pressure from the memory, which may cause it to shut down the OS.

0
source

You can use viewDidUnload to manage memory if your application deserves it.

But don't do

 myInstanceVariable = nil; 

You lose your reference to the memory cell in which the variable values ​​live.

= nil does not free memory. However, your object must be freed. And therefore keepCount and save / release are used.

If you lost your object in viewDidUnload, you cannot release it in dealloc! Attention!!!

If you know what you are doing, you can free and nil your object in viewDidUnload.

ARC:

Using ARC, you should not release manually, even I think you cannot do it. ARC will take care of this. Just use @properties as much as you can with the (weak / strong) attribute to let getters and setters work for you. You can declare @properties in your .m file either (for example, in a class extension).

A simple rule: strong for the objects you want to own, weak for the objects, you do not want to get lost in the retention cycle and own property. ARC does the rest in almost all situations. For example, use weak for delegates.

nil, where you are sure you will not send them a message, otherwise not.

0
source

Source: https://habr.com/ru/post/917553/