Callbacks and object ownership

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.

Figure 27.3  Strong reference cycle

Strong reference cycle

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:

    -​ ​(​v​o​i​d​)​d​e​a​l​l​o​c​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.