Home:ALL Converter>What is the proper way to unload views in iOS 6 in a memory warning (Apple doc flaw)?

What is the proper way to unload views in iOS 6 in a memory warning (Apple doc flaw)?

Ask Time:2012-09-22T14:09:03         Author:XJones

Json Formatter

In iOS 6, viewWillUnload and viewDidUnload are deprecated and UIViewControllers no longer unload views that are not visible on screen during a memory warning. The View Controller Programming Guide has an example of how to manually restore this behavior.

Here is the code sample:

- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];
    // Add code to clean up any of your own resources that are no longer necessary.
    if ([self.view window] == nil)
        // Add code to preserve data stored in the views that might be
        // needed later.

        // Add code to clean up other strong references to the view in
        // the view hierarchy.
        self.view = nil;

Below the code sample is the following note:

The next time the view property is accessed, the view is reloaded exactly as it was the first time.

There is an obvious flaw here. If a view controller that has not loaded its view receives a memory warning it will load its view in the line if ([self.view window] == nil) and then proceed to clean up and release it again. At best, this is inefficient. At worst, it makes the memory conditions worse if a complex view hierarchy and supporting data are loaded. I verified this behavior in the iOS simulator.

I can certainly code around this but seems odd for Apple docs to have such an error. Am I missing something?

Author:XJones,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12541216/what-is-the-proper-way-to-unload-views-in-ios-6-in-a-memory-warning-apple-doc-f