Inherent in any of these callback schemes is the risk of strong reference cycles. Often the object you create has a pointer to the object that is going to call back. And it has a pointer to the object you created. If they each have strong references to each other, you end up with a strong reference cycle – neither of them will ever get deallocated.
Thus, it was decided that:
Notification centers do not own their observers. If an object is an observer, it will typically remove itself from the notification center in its dealloc method:
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
Objects do not own their delegates or data sources. If you create an object that is a delegate or data source, your object should “excuse” itself in its dealloc method:
- (void)dealloc { [windowThatBossesMeAround setDelegate:nil]; [tableViewThatBegsForData setDataSource:nil]; }
Objects do not own their targets. If you create an object that is a target, your object should zero the target pointer in its dealloc method:
- (void)dealloc { [buttonThatKeepsSendingMeMessages setTarget:nil]; }
None of these issues exist in this program because your BNRLogger object will not be deallocated before the program terminates. (Also, in a bit of a fluke, in this exercise you happen to have used two well-documented exceptions to the rules: an NSURLConnection does own its delegate while the connection is running, and an NSTimer does own its target while the timer is valid.)