Friday, May 15, 2015

How Not to Crash #1: KVO and Manual Bindings

Brent Simmons:

Here’s the problem: the binding retains the toObject object. Which means that the button effectively retains the controller. If the controller retains its button (it should), then there’s a retain cycle. Neither will become zombies, but they could become abandoned.

[…]

The solution we came up with fixes the retain cycle without your having to remember to call an invalidate method and call it at the exact right time. It’s safer code.

[…]

In theory, bindings and KVO are there to promote loose coupling, but in practice the coupling is often just as tight — if not tighter, in a sense — and harder to debug and get right.

Like Brian Webster, I don’t recall this being a problem for me, perhaps because I tend to use a different structure. Simmons has a controller binding a button to one of the controller’s own properties. I tend to have controllers that bind widgets to key paths of a separate object controller that holds the model object. The outer controller then owns this object controller. I don’t claim that this is necessarily the best way to do it—I have not used bindings much.

Comments RSS · Twitter

Leave a Comment