Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 862 Bytes

unneeded_notification_center_removal.md

File metadata and controls

43 lines (36 loc) · 862 Bytes

Pattern: Unneeded NotificationCenter removal

Issue: -

Description

Observers are automatically unregistered on dealloc (iOS 9 / macOS 10.11) so you should’t call removeObserver(self) in the deinit.

Examples of correct code:

class Example {
    deinit {
        NotificationCenter.default.removeObserver(someOtherObserver)
    }
}
class Example {
    func removeObservers() {
        NotificationCenter.default.removeObserver(self)
    }
}
class Example {
    deinit {
        cleanup()
    }
}

Examples of incorrect code:

class Foo {
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
class Foo {
    deinit {
        NotificationCenter.default.removeObserver(self,
                                                  name: UITextView.textDidChangeNotification, object: nil)
    }
}