iOS 6 deprecation of viewWillUnload & move to didReceiveMemoryWarning

≡放荡痞女 提交于 2019-11-30 12:03:12

问题


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.


回答1:


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



回答2:


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.




回答3:


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;
    }
}


来源:https://stackoverflow.com/questions/12536715/ios-6-deprecation-of-viewwillunload-move-to-didreceivememorywarning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!