How to use didReceiveMemoryWarning and viewDidUnload?

What is the best practice for using these two memory management techniques? My current application can easily release its views after a memory warning, as it requires a lot of memory. What do I need to do in these methods? I use various feathers in my applications, and I connect the objects in them to my dispatchers without saving them. Do i need to save nib objects when loading the view from the tip and why? I see that the documentation has changed everything from OS 3.0, and I'm only talking about 3.0

+4
source share
2 answers

If you are only talking about iOS 3.0, release any objects that can be easily recreated in viewDidUnload . Then you can recreate or reload them from the tip in viewDidLoad . Make sure that when releasing objects, you will either reset them to zero or ask your setter. That way, you can use viewDidLoad to check for zero or not before rebooting or re-creating.

As for loading from nib objects, if you load your properties and you have the option to save them, then you no longer need to save them.

From Apple :

The UIViewController class provides some automatic processing of low-memory conditions using the didReceiveMemoryWarning method (page 20), which frees up unnecessary memory.

Prior to iPhone OS 3.0, this method was the only way to free up additional memory associated with your custom controller class, but on iPhone OS 3.0 and later versions, the viewDidUnload method (page 30) may be a more suitable place for most needs. When a warning occurs with a low In memory, the UIViewController class clears its views if it knows that it can reload or recreate them later. If this happens, it also calls the viewDidUnload method to give your code the opportunity to give up ownership of any objects associated with your view hierarchy, including objects loaded by the nib file, objects created in your viewDidLoad method (page 29), and objects Created lazily at runtime and added to the view hierarchy. Generally, if your view controller contains output (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to discard ownership of these points or any other view-related data that you no longer need.

+4
source

Like iOS 6, viewDidUnload never called. From Apple :

Prior to iOS 6, when a low memory warning occurred, the UIViewController class cleared its views if it knew that it could reload or recreate them later. If this happens, it also calls viewWillUnload and viewDidUnload to give your code a chance to give up ownership of any objects associated with your view hierarchy, including objects loaded from the nib file, objects created in your viewDidLoad method viewDidLoad and objects created lazily at runtime and added to the view hierarchy. On iOS 6, views are never cleared and these methods are never called. If your view controller must perform certain tasks when memory is low, it must override the doReceiveMemoryWarning method.

+3
source

All Articles