Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 6 deprecation of viewWillUnload & move to didReceiveMemoryWarning

I'm new developer about to launch my first app. I'm confused about the deprecation of viewDidUnload as described below in Apple's iOS 6 release notes:

In iOS 6, the viewWillUnload and viewDidUnload methods of UIViewController are now deprecated. If you were using these methods to release data, use the didReceiveMemoryWarning method instead. You can also use this method to release references to the view controller’s view if it is not being used. You would need to test that the view is not in a window before doing this.

Why is this happening? What guidelines should I follow to ensure that this change doesn't cause any performance issues in my app?

Thanks.

like image 746
Orpheus Mercury Avatar asked Sep 21 '12 19:09

Orpheus Mercury


3 Answers

According to Apple, they have improved the internal memory management for views enough that the gains achieved by destroying stuff in viewWill/DidUnload are minimal. Furthermore, they have data suggesting that many apps crash because the apps do not properly handle those notifications, and do "other" stuff not associated with the view unloading.

Finally, a memory warning is now verified as the first and only warning you will get before your app is terminated due to low memory, so it is really the place to handle memory issues.

So, basically, just remove your viewWillUnload and viewDidUnload methods. Handle memory issues in didReceiveMemoryWarning and any other view controller management in the appropriate places.

EDIT

May I ask: what are those "appropiate places"? I used to use ViewdidUnload in certain situations where view[Will/Did]Disappear were not entirely adequate. Like going further down on the navigation controller stack. Would you mind to elaborate further on that? – Dan1one

That depends. I know that's not what you want to hear, but it's the truth :-)

In general, you should avoid asymmetry. Thus, you should "undo" an operation using the symmetric method from which you "did" the original. In general, you should be able to do all the viewDidUnload type work in didReceiveMemoryWarning and dealloc.

This should really not cause a change, because you had to duplicate most of that code in both of those places anyway.

I don't know what you mean by "going further down on the navigation controller stack" so you will need to clarify that example for me to provide a useful response.

One of the problems with using viewDidDisappear and viewDidAppear was that it was hard to know when the view was appearing because it was actually appearing, or because a view that was on top of it was disappearing... causing it to appear.

These pieces of API are supposed to help you address those issues:

- (BOOL)isMovingFromParentViewController
- (BOOL)isMovingToParentViewController
- (BOOL)isBeingDismissed
- (BOOL)isBeingPresented
like image 157
Jody Hagins Avatar answered Nov 05 '22 10:11

Jody Hagins


In iOS 6, views are never unloaded.

This means that loadView and viewDidLoad are only ever called once, and viewDidUnload is never called. So if your view controller uses viewDidUnload to handle low memory conditions then it will need to change.

If you want to respond to low memory conditions, implement didReceiveMemoryWarning and release your temporary data and objects in this method.

like image 27
Darren Avatar answered Nov 05 '22 09:11

Darren


In iOS 6 we should release views by ourself, do something like this

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

    if([self isViewLoaded] && self.view.window == nil)
    {
        self.view = nil;
    }
}
like image 40
mrbb Avatar answered Nov 05 '22 09:11

mrbb