Skip to content

Files

Latest commit

 

History

History
41 lines (29 loc) · 968 Bytes

notification_center_detachment.md

File metadata and controls

41 lines (29 loc) · 968 Bytes

Pattern: Unsafe NotificationCenter detachment

Issue: -

Description

Warns when an object removes itself as an observer to all notifications. This can be a problem if a superclass or a subclass wants to keep observing some notifications.

An object should only remove itself as an observer in deinit.

Examples of correct code:

class Foo { 
   deinit {
       NotificationCenter.default.removeObserver(self)
   }
}


class Foo { 
   func bar() {
       NotificationCenter.default.removeObserver(otherObject)
   }
}

Examples of incorrect code:

class Foo { 
   func bar() {
       NotificationCenter.default.removeObserver(self)
   }
}

Further Reading