
View Controller Garbage Collection
==================================

Due to use of TTSwapMethods in Three20UI and Apple's denial of its use, we have had to remove
the method swizzling of dealloc in `UIViewController`. The dealloc method was swapped because
we had needed to override the default dealloc for all `UIViewController` instances. This made it
possible to do some necessary cleanup when a controller was released.

Now that we can't override `UIViewController`'s dealloc globally, we need a more creative solution.
The solution proposed here is one that uses a lightweight form of garbage collection to provide
similar functionality.

How it works
------------

To begin, `TTViewController` has now been split into a more base implementation,
`TTBaseViewController`, which can be found in UICommon. This base view controller does what
the old method swizzling did in the dealloc, but now it's a safer implementation due to
proper inheritance.

Some functionality of TTViewController has also been placed in the new `TTNavigatorViewController`,
which inherits from `TTBaseController`. `TTViewController` itself inherits from
`TTNavigatorViewController`. The inheritance diagram follows.

       [UICommon] TTBaseViewController
                         ^
                         |
    [UINavigator] TTNavigatorViewController
                         ^
                         |
             [UI] TTViewController

This is only half of the solution, however, because we need to consider that the original
method swizzling allowed us to dealloc the necessary global properties on *all*
`UIViewControllers`, not just those that inherit from what is now `TTBaseViewController`.

In order to ensure that all `UIViewControllers` properly execute their shutdown logic, we
start a timer whenever a `UIViewController` is added to any of the global data structures.
This timer checks the retain count of the controller every n seconds. If the only thing
retaining this object is the garbage collector, we can safely call the shutdown logic and
release the view controller entirely.

Repercussions of this new design
--------------------------------

Garbage collection is a term that I'm personally scared of. For me it implies loss of control
over the management of memory. However, in this particular case we can rest easy because we
have direct control over the garbage collector. If you are afraid of a view controller not
being released in a timely manner, or perhaps you've received a `didReceiveMemoryWarning`
notification, then you simply need to call

    [TTViewController doGarbageCollection]

This will force both the UINavigator and UICommon components to initiate their garbage collection.

Advantages of using TTViewController
------------------------------------

If all of your view controllers inherit from `TTViewController`, then the garbage collector will
never actually be needed because `TTViewControllers` properly shut themselves down in their
dealloc.


This article written by:  
-- Jeff Verkoeyen (@featherless) *Created on May 28, 2010.*
