简体   繁体   English

在字典中对子视图的强烈引用会导致引用周期吗?

[英]Will a strong reference to a subview in a dictionary cause a reference cycle?

I have a view with variable subviews, the subviews are set up using a enum describing the type of this subview. 我有一个带有可变子视图的视图,这些子视图使用描述此子视图类型的枚举来设置。 My question is if the following would cause a strong reference cycle or if there is a better way to do this: 我的问题是,以下内容是否会导致强大的参考周期,或者是否有更好的方法可以做到这一点:

class ControlBar: UIView {

    var item = [ControlBarItemType : ControlBarItem]()

    func set(with types: [ControlBarItemType]) {

        for type in types {
            let newItem = ControlBarItem(frame: CGRect(), type: type)
            //constraints and stuff
            self.addSubview(newItem)
            item[type] = newItem

        }
    }
}

I can't declare the dictionary as weak. 我不能宣布字典为弱字典。 So the superview will have a reference to each ControlBarItem in the subview hierarchy and also with this dictionary, indexed by type. 因此,该超级视图将对子视图层次结构中的每个ControlBarItem以及该字典(按类型索引)进行引用。 My reason for this is occasionally I need to change the state of the BarItem from the viewController that acts as the delegate for ControlBar. 我这样做的原因是有时我需要从充当ControlBar委托的viewController更改BarItem的状态。

Nope, that wouldn't cause a retain cycle. 不,那不会引起保留周期。

In your case, there are two references being created 在您的情况下,正在创建两个引用

  1. ControlBar has a reference to the ControlBarItem from adding it as a subviews ControlBar通过将其添加为子视图来引用ControlBarItem
  2. ControlBar has a reference your item dictionary which has a reference to the ControlBarItem ControlBar有一个引用,即您的item字典,其中有对ControlBarItem的引用

In both cases, the reference only goes from ControlBar -> ControlBarItem . 在这两种情况下,引用仅来自ControlBar > ControlBarItem You would only have to worry about a reference cycle if there were any references going from ControlBarItem -> ControlBar 如果ControlBarItem > ControlBar有任何引用,则只需担心引用周期

You are NOT creating a strong reference cycle. 您创建的参考周期强。

Infact you have 2 strong reference from the ControlBar to each subview. 实际上,您从ControlBar到每个子视图都有2个强引用。 This is not a problem. 这不是问题。

Instead, if you had a strong reference from ControlBar to the subviews AND a strong reference from the subviews to the ControlBar, you would have a strong reference cycle. 相反,如果您从ControlBar对子视图有强引用,而从子视图对ControlBar有强引用,那么您将拥有一个强引用周期。

在此处输入图片说明

To think about this productively, keep it simple. 要高效地进行思考,请保持简单。 Look for the cycle: 寻找周期:

  • ControlBar is preserving strong references to ControlBarItems. ControlBar保留对ControlBarItems的强引用。

  • So the question to ask yourself is: does a ControlBarItem also preserve a strong reference to its containing ControlBar? 所以要问自己的问题是: ControlBarItem是否也保留对其包含ControlBar的强引用?

If the answer is yes, you have a retain cycle. 如果答案是肯定的,则您有一个保留周期。 If the answer is no, you don't. 如果答案是否定的,那么您不会。

And it appears that the answer is no. 看来答案是否定的。

(To elaborate: A view has no strong reference to its superview. And I don't see you handing the ControlBarItem a strong reference to its containing ControlBar. If you did need to give the ControlBarItem a reference to its containing ControlBar, that would be the time to worry: you would want to make sure that this was not a strong reference.) (详细说明:视图没有对其超级视图的强烈引用。而且我看不到您 ControlBarItem对其包含ControlBar的强烈引用。如果确实需要为ControlBarItem提供对其包含ControlBar的引用, 将是需要担心的时间:您需要确保这不是一个强有力的参考。)

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

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