# Notification issue in Combine

**URL:** <https://forums.kodeco.com/t/notification-issue-in-combine/196827>\
**Category:** Swift\
**Created:** [October 2, 2023, 2:38am UTC](https://forums.kodeco.com/t/notification-issue-in-combine/196827 "2023-10-02T02:38:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diuming](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/diuming/32/110937_2.png) [@diuming](https://forums.kodeco.com/u/diuming)\
**Post date:** [October 2, 2023, 2:38am UTC](https://forums.kodeco.com/t/notification-issue-in-combine/196827/1 "2023-10-02T02:38:58Z")

</div>

I do a simple notification test with combine.  
Click `postNotification` button in ViewController that received notification successful.  
Click `removeReceiver` button (set \_receiver to nil) in ViewController that NotificationReceiver object never deinit. Why?  
Do I need to do more for releasing NotificationReceiver object in ViewController?  
Could you give me some advice. thanks

thanks

```auto
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")
}

```

```auto
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
    }
}

```

---

<div class="post-metadata">

**Author:** ![gdelarosa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gdelarosa/32/109063_2.png) [@gdelarosa](https://forums.kodeco.com/u/gdelarosa)\
**Post date:** [October 19, 2023, 9:36pm UTC](https://forums.kodeco.com/t/notification-issue-in-combine/196827/2 "2023-10-19T21:36:35Z")

</div>

Hi @diuming! Thank you for sharing your code and sorry to hear that you’re running into an issue. Looking at your code it seems as though the `NotificationReceiver` object is never deinitialized because the \_observer property holds a strong preference to the Combine publisher. You can try to cancel the `_observer` subscription when you set the `_receiver` property to `nil`. Calling the `cancel( )` method on the `_observer` property may do the trick. Here is an example with your code,

```auto
class ViewController: NSViewController {
    var _receiver: NotificationReceiver? = NotificationReceiver()
    
    @IBAction func postNotification(_ sender: Any) {
        NotificationCenter.default.post(name: .userClick, object: nil)
    }
    
    @IBAction func removeReceiver(_ sender: Any) {
        _observer?.cancel()
        _receiver = nil
    }
}

```

Something else you can try is capturing a weak reference to `self` in the closure, which should break the retain cycle and deallocating the `NotificationReceiver`.

```auto
class NotificationReceiver {
    var _observer: AnyCancellable?

    deinit {
        print("[NotificationReceiver] deinit")
    }

    init() {
        _observer = NotificationCenter.default.publisher(for: .userClick, object: nil)
            .sink { [weak self] notification in
                print("Received notification: \(notification)")
            }
    }
}

```

Then you can call `_receiver = nil` in the view controller. Hope this helps!

---

<div class="post-metadata">

**Author:** ![diuming](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/diuming/32/110937_2.png) [@diuming](https://forums.kodeco.com/u/diuming)\
**Post date:** [October 20, 2023, 2:16am UTC](https://forums.kodeco.com/t/notification-issue-in-combine/196827/3 "2023-10-20T02:16:51Z")

</div>

Hi gdelarosa,

I forget strong preference in closure.  
`sink { [weak self] notification in` is better for me.

thanks

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 16, 2024, 6:39pm UTC](https://forums.kodeco.com/t/notification-issue-in-combine/196827/4 "2024-03-16T18:39:30Z")

</div>

Closed on March 16, 2024, 6:39pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
