Should vars be weak

In view controller A I have things like a UITableView, UISearchController, and the UINavigationController.

I also have an NSObject subclass for my delegates that needs to know about those three items, and then I assign that NSObject as the data source and delegate on the table view and he searchResultsUpdater on the search controller.


If I store those as a 'let' variable in the NSObject subclass, have I created a retain cycle? I'm wondering if I need to store them as weak vars instead.

Accepted Reply

If I store those as a 'let' variable in the NSObject subclass, have I created a retain cycle?

No. As you’ve described it the only cycle is between your NSObject subclass and the UITableView (and friends) via its delegate reference. However, that delegate reference is already weak.

Honestly, though, I wouldn’t take my word for it; rather, construct the layout you want and test whether you have a retain cycle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

If I store those as a 'let' variable in the NSObject subclass, have I created a retain cycle?

No. As you’ve described it the only cycle is between your NSObject subclass and the UITableView (and friends) via its delegate reference. However, that delegate reference is already weak.

Honestly, though, I wouldn’t take my word for it; rather, construct the layout you want and test whether you have a retain cycle.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

If only I could figure out how to use Instruments i would 😟

You should take a look at Xcode 8’s snazzy new memory graph facility. WWDC 2016 Session 410 Visual Debugging with Xcode has the details.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks man. That was helpful! Actually found a memory leak with it already 🙂