0

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. 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.

twiz_
  • 1,178
  • 10
  • 16

3 Answers3

0

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
  2. ControlBar has a reference your item dictionary which has a reference to the ControlBarItem

In both cases, the reference only goes from ControlBar -> ControlBarItem. You would only have to worry about a reference cycle if there were any references going from ControlBarItem -> ControlBar

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
0

You are NOT creating a strong reference cycle.

Infact you have 2 strong reference from the ControlBar to each subview. 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.

enter image description here

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • Despite three good answers, you get the check for the pretty picture! Thanks everyone. – twiz_ Nov 18 '16 at 18:16
  • Just to briefly elaborate, I was afraid something was unique about a dictionary that I could not declare it weak, in which case both the ControlBar and the dictionary would have a reference to the item and maybe then I have a problem. I was pretty sure that all my reference directions worked out. Thanks again. – twiz_ Nov 18 '16 at 18:18
0

To think about this productively, keep it simple. Look for the cycle:

  • ControlBar is preserving strong references to ControlBarItems.

  • So the question to ask yourself is: does a ControlBarItem also preserve a strong reference to its containing 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.)

matt
  • 515,959
  • 87
  • 875
  • 1,141