Ahmed Amine
Ahmed Amine

Reputation: 47

Remove Observer Notification Swift 3

I use Kugel Library for Notification (https://github.com/TakeScoop/Kugel/tree/swift-3.0). I want to know how to remove Observer and where in my code . i use Unsubscribe for library and nothing happens

override func viewDidDisappear(_ animated: Bool) {

    super.viewDidDisappear(animated)
    Kugel.unsubscribe("SleepMode")
    Kugel.unsubscribe("SleepModeSynchroMode")
    Kugel.unsubscribe(self, name: NSNotification.Name(rawValue: "SleepMode"), object: nil)
    Kugel.unsubscribe(self, name: NSNotification.Name(rawValue: "SleepModeSynchroMode"), object: nil)
    NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "SleepMode"), object: nil);
    NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "SleepModeSynchroMode"), object: nil);
}

i want remove subscribe notification ( add observer ) when i go back to other view. I use denit { } but the notification that doesn't killed .

Can you helpme

Tahnks

Upvotes: 2

Views: 8097

Answers (3)

Gabriel
Gabriel

Reputation: 213

All that is wrong. Here is the right way to remove observers in Swift (also applies to Obj-C): According to Apple's documentation, you have to keep a reference to your observer! NSNotificationCenter addObserver Self is not an observer so NotificationCenter.default.removeObserver(self, name: Notification.Name(rawValue: "SleepMode"), object: nil); doesn't do anything. What you have to do is:

  1. Extend the Notification.Name for your notification: (where you post the notification)

    extension Notification.Name {
      static let notification = Notification.Name(rawValue: "A notification")
    }
    
  2. Create a weak reference to your observer with:

    weak var observer: NSObjectProtocol?
    
  3. Create an "addObserver" function like so: (where you want to listen to the notification)

    func addObserver() {
      guard observer == nil else { return }
      observer = NotificationCenter.default.addObserver(forName: .notification,
                                                         object: nil,
                                                          queue: .main) { notification in
        print("Notification triggered")
    }
    
  4. Create a "removeObserver" function:

    func removeObserver() {
      guard let observer = observer else { return }
      NotificationCenter.default.removeObserver(observer)
    }
    
  5. Call your "addObserver" function from wherever you need it in your code (most probably from your viewDidLoad method)

  6. Call the "removeObserver" function when you are done listening to that notification.

An important point here is that if you have an extra strong reference to your class implementing the notification and you "think" the observer is removed but it is not, then the guard implementation above prevents your code from creating several observers. This is particularly the case for some implementations of addObserver in the viewDidLoad function missing a removeObserver. A proof? add a breakpoint in the addObserver function at the line where you assign the observer and edit the breakpoint (right click) then choose add action and pick Sound and select the option Automatically continue after evaluating actions.

enter image description here

Start your app and go back and forth in the view that implements the observer. If the number of time you hear the sound is constant, you're done! Otherwise you should here the sound increase by one each time you enter the view. There you go!

Upvotes: 5

iknowNothing
iknowNothing

Reputation: 389

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its deallocation method. If your app targets earlier releases, you need to keep a reference to the observer object and submit it instead of 'self', also the library says its been Deprecated why are you using it?

Upvotes: 3

Vandal
Vandal

Reputation: 780

Try and remove the observer in viewWillAppear

Upvotes: -3

Related Questions