Group Group Group Group Group Group Group Group Group

Ownership cycle

#1

In chapter 30 section “Remove notification observers” you speak of an ownership cycle. Was this cycle already in effect when you coded
NotificationCenter.default.addObserver(forName: . . .
or did it became reality when you added a stored property observer and coded
observer = NotificationCenter.default.addObserver(forName: . . .
?

#2

Do note (as the book indicates) that notification observers are automatically removed for you now and the code from the relevant section is no longer necessary :slight_smile:

That said, ownership is added whenever you call NotificationCenter.default.addObserver since that sets up a relationship between the NotificationCenter and the receiver of notifications. Once you add an observer, the NotificationCenter keeps notifying that observer of relevant notifications till the observer is removed from the NotificationCenter.

#3

Calling NotificationCenter.default.addObserver does add a strong reference from the observer to self. My question is whether self has a strong reference to the observer if the observer is not stored as property in self.

#4

Calling NotificationCenter.default.addObserver does add a strong reference from the observer to self. My question is whether self has a strong reference to the observer if the observer is not stored as property in self.