ViewWillDisappear versus dealloc

后端 未结 2 648
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 20:41

I put an NSlog in my dealloc method of my view controller. It does not get consistently called. I do notice that ViewWillDisappear does get called always. Would it be ok to move

2条回答
  •  一生所求
    2021-02-10 20:59

    Well that depends.

    If you need to reduce the memory footprint of your app, unloading stuff while the view is not visible via viewWillDisappear is a good way to go. However you'll need to re-initalize everything once the view will be shown again, depending on its content this may produce considerable overhead - maybe even without the need.

    For a small app (in terms of memory usage), using dealloc to unload your stuff is fine. It will be called only if the objects retain count drops to zero and is the last method that will be run before the object is destroyed. When using autorelease, that may not be the case right away and of course the object could be retained by some object other than the parentviewcontroller, preventing it from being destroyed and thus dealloc from being called.

    You might want to check out the Memory Management Programming Guide, it'll explain things more detailed.

提交回复
热议问题