简体   繁体   English

iOS中强烈而微弱的困惑

[英]Strong and Weak Confusion in iOS

I am little confused about using Strong or Weak in my particular case. 在我的特殊情况下,我对使用StrongWeak感到困惑。

I have one class ParentClass which has 3 object ContainerClass1 , ContainerClass2 and ContainerClass3 . 我有一个ParentClass类,它有3 object ContainerClass1ContainerClass2ContainerClass3

Each ContainerClass has its own strong properties with Mutable objects like NSMutableArray 每个ContainerClass都有自己的强大属性,其中包含像NSMutableArray这样的Mutable对象

Now in my case, I have to show only one ContainerClass at a time, so if ContainerClass1 is shown then ContainerClass2 and ContainerClass3 is not required. 现在,在我的情况,我也只显示一个ContainerClass的时间,所以如果ContainerClass1显示然后ContainerClass2ContainerClass3不是必需的。

So I thought when I show ContainerClass1 , will set ContainerClass2 and ContainerClass3 objects to nil . 所以我想当我展示ContainerClass1 ,会将ContainerClass2ContainerClass3对象设置为nil Here I am confused whether just setting the other ContainerClass (not shown) to nil will release its memory? 在这里我很困惑,只是将其他ContainerClass (未显示)设置为nilrelease其内存吗? because they have strong properties to other objects. 因为它们对其他对象具有强大的属性。

Or should I need to set all other ContainerClass's strong properties to nil first and then set ContainerClass to nil ? 或者我是否需要先将所有其他ContainerClass's强属性设置为nil ,然后将ContainerClass设置为nil

Thanks in advance. 提前致谢。

@zoeb, may this link will help you to keep away from basic memory problems. @zoeb,这个链接可能会帮助您远离基本的内存问题。

how-to-overcome-memory-problems-in-iphone-applications-with-automatic-reference-counting 如何对克服记忆,问题式,iphone的应用程序,具有自动引用计数

Edited: 编辑:

As we know that Apple introduced ARC in IOS 5.0, ARC is compiler level feature that simplifies process of lifetime of objective-c objects. 我们知道Apple在IOS 5.0中引入了ARC,ARC是编译器级功能,可以简化objective-c对象的生命周期。 Before ARC introduced, We managed memory manually means “Manual Reference Counting(MRC)” . 在ARC介绍之前,我们手动管理内存意味着“手动引用计数(MRC)”。 With MRC, Developer need to remember when to release or retain object. 使用MRC,Developer需要记住何时释放或保留对象。 Means that Developer need to manage life cycle of objective-c objects. 意味着Developer需要管理objective-c对象的生命周期。

According to Developer perspective, We are mostly interested to adding new features in our application rather then focusing on memory issues. 根据Developer的观点,我们最感兴趣的是在我们的应用程序中添加新功能,而不是专注于内存问题。 But the things is sure that memory management perform crucial role in application success. 但事情确信内存管理在应用程序成功中发挥着至关重要的作用。 To Provide help to Developer, Apple was figure out the way of automatically manage memory. 为了向Developer提供帮助,Apple找到了自动管理内存的方法。

ARC is smartly manage memory but this is not 100 percent. ARC巧妙地管理内存,但这不是百分之百。 We need to focus on some points while development to remove our application from lack of memory problem. 我们需要在开发过程中关注一些问题,以便从缺乏内存问题中删除我们的应用程序。 Here i will try to provide solution of manage memory in ARC base application. 在这里,我将尝试在ARC基础应用程序中提供管理内存的解决方案。 that is not 100 percent as well. 这也不是百分之百。 but its will try to help compiler to estimate life cycle of objective object. 但它会尽力帮助编译器估算客观对象的生命周期。

Here are the some steps that you need to implement in your every controllers. 以下是您需要在每个控制器中实现的一些步骤。

Step 1. Declare weak property to every UI Controls that used in application. 步骤1.向应用程序中使用的每个UI控件声明弱属性。

Example : 示例:
@property (nonatomic, weak) IBOutlet UIButton* btnPost;

@property (nonatomic, weak) IBOutlet UITableView* tblMessages;

etc. 等等

Step 2. As per our developer most confusing question is that whether compiler allow to declare “dealloc” method in ARC base application. 步骤2.根据我们的开发人员,最令人困惑的问题是编译器是否允许在ARC基础应用程序中声明“dealloc”方法。 the answer is yes but don't allowed to declare “[super dealloc]” inside it. 答案是肯定的,但不允许在其中声明“[super dealloc]”。 so override “dealloc” method in every controllers. 所以在每个控制器中覆盖“dealloc”方法。

-(void)dealloc{

}

Step 3. Remove heavy loaded object from superview in “dealloc” method rather then setting just “nil” reference like MKMapview, ScrollView etc. 步骤3.在“dealloc”方法中从superview中删除重载对象,而不是像MKMapview,ScrollView等那样设置“nil”引用。

-(void)dealloc{
dictAddress = nil;
arrayList = nil;
[map removeFromSuperview];
[scrollView removeFromSuperview];
}

Step 4. Avoid dead lock mechanism. 步骤4.避免死锁机制。 (Example : Class A and Class B is there. Class B is declared Delegate with property type “Strong”. so that Class A and Class B dependent on each other on one is going to release. so in that case “dealloc” method is not called of either classes. so that class keep in memory. to removed such cases we need to keep “Assign” reference to Delegate object.) this is just for example. (示例:类A和类B在那里。类B被声明为Delegate,属性类型为“Strong”。因此,A类和B类在一个上相互依赖将会释放。因此在这种情况下,“dealloc”方法是没有调用任何类。所以类保留在内存中。为了删除这种情况,我们需要保持对Delegate对象的“赋值”引用。)这只是例如。 We need to consider other things as well like “Keep weak reference for block so it will release objects once its execution completed”. 我们还需要考虑其他事情,例如“保持块的弱引用,以便在执行完成后释放对象”。

these are the basic steps that avoiding memory problems. 这些是避免内存问题的基本步骤。 Still if you face memory problems then you need to take help of Analyzer to find leak and memory usage. 如果您遇到内存问题,那么您需要借助Analyzer来查找泄漏和内存使用情况。

Below link will help you to analyze memory. 下面的链接将帮助您分析内存。

Mamory analyzer Mamory分析仪

The confusion between strong and weak will be clear with the below link. 通过以下链接可以清楚地看出强弱之间的混淆。

Differences between strong and weak in Objective-C Objective-C中强弱之间的差异

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

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