简体   繁体   English

iOS:什么时候最好在视图中释放 model object?

[英]iOS: When is best to deallocate a model object in a view controller?

I've got a fairly basic question here.我在这里有一个相当基本的问题。 I find that quite often I instantiate model objects in the viewDidLoad: method of view controllers, say in the case of a web service object that is used to populate the elements of a table in the view controller: I find that quite often I instantiate model objects in the viewDidLoad: method of view controllers, say in the case of a web service object that is used to populate the elements of a table in the view controller:

- (void)viewDidLoad {
    [super viewDidLoad];    
    itemService = [[BlogItemService alloc] init];
}

Where should I release itemService?我应该在哪里发布 itemService? In viewDidUnload or dealloc?在 viewDidUnload 或 dealloc 中?

Furthermore, is it common to allocate objects like this in viewDidLoad?此外,在 viewDidLoad 中分配这样的对象是否常见? Is there not a more suitable init type method?难道没有更合适的init类型方法吗?

Update: I have a particular concern.更新:我特别担心。 Let's say I deallocate itemService in dealloc.假设我在 dealloc 中取消分配 itemService。 If the view is unloaded and then reloaded, but the view controller is not deallocated, won't I have a memory leak as the previous instance of itemService is orphaned when the new one is instantiated?如果视图被卸载然后重新加载,但视图 controller 没有被释放,我会不会有 memory 泄漏,因为 itemService 的前一个实例在实例化新实例时是孤立的?

Where should I release itemService?我应该在哪里发布 itemService? In viewDidUnload or dealloc?在 viewDidUnload 或 dealloc 中?

if the object is lightweight or takes a long time to create, do it in dealloc.如果 object 是轻量级的或需要很长时间才能创建,请在 dealloc 中执行。 if it consumes a lot of memory, then use matching pairs in viewDidLoad/viewDidUnload.如果它消耗了大量的 memory,那么在 viewDidLoad/viewDidUnload 中使用匹配对。

Furthermore, is it common to allocate objects like this in viewDidLoad?此外,在 viewDidLoad 中分配这样的对象是否常见?

yes是的

Is there not a more suitable init type method?难道没有更合适的init类型方法吗?

the designated initializer (in some cases)指定的初始化器(在某些情况下)

Update: I have a particular concern.更新:我特别担心。 Let's say I deallocate itemService in dealloc.假设我在 dealloc 中取消分配 itemService。 If the view is unloaded and then reloaded, but the view controller is not deallocated, won't I have a memory leak as the previous instance of itemService is orphaned when the new one is instantiated?如果视图被卸载然后重新加载,但视图 controller 没有被释放,我会不会有 memory 泄漏,因为 itemService 的前一个实例在实例化新实例时是孤立的?

to avoid this, use:为避免这种情况,请使用:

BlogItemService * item = [[BlogItemService alloc] init];
self.itemService = item;
[item release], item = nil;


You should release the object in dealloc您应该在 dealloc 中释放 object

You can allocate memory using:您可以使用以下方法分配 memory:

itemService=[BlogItemService new];  

If you would ask me the best place to initialize model objects is in the init method of the class and release it in dealloc .如果您问我初始化 model 对象的最佳位置是在 class 的init方法中并在deallocrelease它。

It is very uncommon situation, but if you unload and then load view again you will have leak just as you suggested.这是非常罕见的情况,但是如果您卸载然后再次加载视图,您将按照您的建议进行泄漏。 So where to allocate objects it is a good question and it rather depends on your choice.所以在哪里分配对象是一个很好的问题,它取决于你的选择。 Probably it is better to load objects every time view is loaded when they are very heavy and some small objects will probably be easier to load in init.每次加载视图时加载对象可能会更好,因为它们非常重,并且一些小对象可能更容易在 init 中加载。 But you have also handle your view controller carefully if you want to load and unload view many times, beacuse common pattern is to destroy controller every time the view dissapears (and mostly it is a good solution).但是,如果您想多次加载和卸载视图,您也必须小心处理您的视图 controller,因为常见的模式是每次视图消失时都销毁 controller(而且大多数情况下这是一个很好的解决方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM