I do a simple notification test with combine.
Click postNotification
button in ViewController that received notification successful.
Click removeReceiver
button (set _receive to nil) in ViewController that NotificationReceiver object never deinit. Why?
Do I need to manual to cancel?
thanks
class NotificationReceiver {
var _observer: AnyCancellable?
deinit {
print("[NotificationReceiver] deinit")
}
init() {
_observer = NotificationCenter.default.publisher(for: .userClick, object: nil)
.sink { notification in
print("Received notification: \(notification)")
}
}
}
extension Notification.Name {
static let userClick = Notification.Name("UserClick")
}
class ViewController: NSViewController {
var _receiver: NotificationReceiver? = NotificationReceiver()
@IBAction func postNotification(_ sender: Any) {
NotificationCenter.default.post(name: .userClick, object: nil)
}
@IBAction func removeReceiver(_ sender: Any) {
_receiver = nil
}
}
1 post - 1 participant