简体   繁体   English

ios 对强/弱引用感到困惑

[英]ios confused about strong/weak references

I've looked through the questions on strong/weak references, and understand the reason for using weak (the parent to child relationship).我查看了有关强/弱引用的问题,并了解使用弱(父子关系)的原因。 However, I'm confused about specific scenarios where a parent to child relationship is created.但是,我对创建父子关系的特定场景感到困惑。

For example, is adding subviews to a UIView object..an example of creating a parent/child relationship?例如,是否将子视图添加到 UIView 对象...创建父/子关系的示例? What is?什么是?

So far, I did everything in my project using strong, nowhere have I used weak, but I'm not sure if I'll run into memory management issues (or how to even check if I will).到目前为止,我在我的项目中使用strong 做了所有事情,我没有使用过weak,但我不确定我是否会遇到内存管理问题(或者如何检查我是否会遇到)。

Can anyone provide specific situations or examples where a parent to child relationship is created?任何人都可以提供创建父子关系的具体情况或示例吗?

Thanks!谢谢!

EDIT: In fact, I am getting some "Received Memory Warning" problems in one of my ViewControllers that displays a lot of data (map view, number of images, text, buttons).编辑:事实上,我在一个显示大量数据(地图视图、图像数量、文本、按钮)的 ViewController 中遇到了一些“收到的内存警告”问题。 Everything property has a strong pointer.所有属性都有一个强指针。 I need to fix my memory management issues for this ViewController我需要修复这个 ViewController 的内存管理问题

You're understanding is backwards.你的理解是倒退的。 Weak references are more often used for implementing child-to-parent relationships.弱引用更常用于实现子父关系。 They would seldom make sense for a parent-to-child relationship.对于父母与孩子的关系,它们很少有意义。 Generally the parent owns the child;通常父母拥有孩子; that means strong.这意味着强大。

The vast majority of the time you want a strong reference.绝大多数情况下,您需要一个强有力的参考。 That's why it's the default.这就是为什么它是默认值。 The most common reason not to have a strong reference is if it would cause a retain loop.没有强引用的最常见原因是它是否会导致保留循环。 For instance, if A has a strong reference to B, then if B had a strong reference to A you'd have a loop and neither object would ever be deallocated.例如,如果 A 对 B 有一个强引用,那么如果 B 对 A 有一个强引用,你就会有一个循环,并且两个对象都不会被释放。 So you pick one of the objects to be the owner, and it has the strong reference.因此,您选择其中一个对象作为所有者,并且它具有强引用。 The other object has a weak reference.另一个对象具有弱引用。

The most common case of this is delegation.最常见的情况是委托。 A delegate almost always owns the thing it is delegate for.委托几乎总是拥有它所委托的东西。 So the delegating object should have a weak reference to the delegate.所以委托对象应该有一个对委托的弱引用。 As a convention in Objective-C, a property called delegate is expected to be weak.作为Objective-C 中的约定,一个名为delegate的属性应该是弱的。 (If this feels backwards, think about how you use UITableView and UITableViewDelegate in practice, and which one you'd want to consider the "owner.") (如果这感觉倒退,请考虑在实践中如何使用UITableViewUITableViewDelegate ,以及您希望将哪一个视为“所有者”。)

Weak delegate pointers are not a hard-and-fast rule.弱委托指针不是硬性规定。 There are exceptions such as NSURLConnection .有一些例外,例如NSURLConnection If the delegating object has a shorter lifetime than the delegate, then it is ok (and generally preferable) for it to maintain a strong reference.如果委托对象的生命周期比委托对象的生命周期短,那么它可以(并且通常更可取)保持强引用。

"Received Memory Warning" does not necessarily have anything to do with memory management. “收到内存警告”不一定与内存管理有关。 It just means you're using too much memory.这只是意味着您使用了太多内存。 If you have retain loops, then you may be leaking memory, and that would cause this warning.如果您有保留循环,那么您可能会泄漏内存,这将导致此警告。 But it could also be because you're simply using too much memory.但这也可能是因为您只是使用了太多内存。 The "Allocations" tool in Instruments is the best way to investigate this. Instruments 中的“分配”工具是对此进行调查的最佳方法。

While the implementation of "strong" and "weak" are very recent additions to Objective-C, they just formalize and provide better language support for what properly written code has been doing for many years with manual retains.虽然“强”和“弱”的实现是最近添加到 Objective-C 的,但它们只是形式化并为正确编写的代码多年来使用手动保留所做的事情提供了更好的语言支持。 The ownership patterns are identical today to what they were before ARC.今天的所有权模式与 ARC 之前的所有权模式相同。

Some people put together a very helpful diagram explaining when to use weak references on the "Coding Together" Piazza class.有些人整理了一个非常有用的图表,解释了何时在“一起编码”Piazza 类上使用弱引用。 It has some great diagrams explaining basic memory management with strong/weak pointers.它有一些很好的图表,用强/弱指针解释了基本的内存管理。

http://piazza-uploads.s3-website-us-east-1.amazonaws.com/attach/h3ex5vh7htrh9/h4777mrpwp17bg/h4777tczi147de/Views,%20Outlets,%20Weak%20References.pdf http://piazza-uploads.s3-website-us-east-1.amazonaws.com/attach/h3ex5vh7htrh9/h4777mrpwp17bg/h4777tczi147de/Views,%20Outlets,%20Weak%20References.pdf

When you add a subview to a view, the parent will retain it's subview under the covers.当您向视图添加子视图时,父视图将在幕后保留其子视图。 Yes this is a parent child relationship.是的,这是父子关系。 If you are creating your own UIView subclasses, you will be doing the same.如果您正在创建自己的 UIView 子类,您也会这样做。

Parents have a strong relationship with their children, but children should never have a strong relationship on their parents.父母与孩子的关系很牢固,但孩子永远不应该对父母有很强的关系。 This can lead to retain cycles, where neither can be released because they both have a strong connection with one another.这可能导致保留循环,其中任何一个都不能被释放,因为它们彼此之间有很强的联系。

Strong is the default, you will tend to use weak when you are using the delegate pattern. Strong 是默认设置,当您使用委托模式时,您会倾向于使用 weak。

Maybe you should post more specific info about the problem you are having with the received memory warning, as this question is quite general about memory semantics.也许您应该发布有关您收到的内存警告问题的更具体的信息,因为这个问题是关于内存语义的非常普遍的问题。

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

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