IOS failed to delete observer notifications. Deinit not getting a call

I have a UIView similar to the one you see below:

class ViewTaskViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { override func viewDidLoad() { super.viewDidLoad() subscribeToNotifications() } func subscribeToNotifications() { let notification = NotificationCenter.default notification.addObserver(forName: Notification.Name(rawValue: "TimerUpdated"), object: nil, queue: nil, using: handleUpdateTimer) print("Subscribed to NotificationCenter in ViewTaskViewController") } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) print("TUFU TUFU TUFU") NotificationCenter.default.removeObserver(self) } deinit { print("DENINT") } @objc func handleUpdateTimer(notification: Notification) { if let userInfo = notification.userInfo, let timeInSeconds = userInfo["timeInSeconds"] as? Int { withUnsafePointer(to: &self.view) { print("We got timeeeeee \(timeInSeconds) \($0)") } //do something here.... } } 

}

The problem I am facing is that I cannot remove observers from this particular UIView when the user clicks the back button and returns to another viewController.

ViewWillDisppear , but deinit not called. It is strange that if we remove subscribeToNotifications() from viewDidLoad() , then deinit is called.

Another problem is memory leak. As you can see in the screenshot below, when the view subscribes to notifications, and the user leaves / returns to the view, memory usage increases. enter image description here

Now compare this to the fact that when subscribeToNotifications() commented out, the increase in memory usage and only one instance of viewController does not increase. enter image description here The conclusion is that there seems to be a correlation between creating a subscription to notify a new instance of UIView, so deinit not called.

I would like to know if there is a way to de-initialize the submission and unsubscribe from the notification.

Please let me know if you need more information. :)

+8
ios memory-leaks swift nsnotification deinit
source share
3 answers

I found that removeObserver () only works if you use this version of addObserver ()

 notification.addObserver(self, selector:#selector(self.handleUpdateTimer), name: Notification.Name(rawValue: "TimerUpdated"), object: nil) 

I assume that in the original version you do not actually indicate who the observer is.

+4
source share

Like @Spads said you can use

 NotificationCenter.default.addObserver(self, selector: #selector(subscribeToNotifications), name: NSNotification.Name(rawValue: "TimerUpdate"), object: nil) 

or the one you already have. you can delete your notification by this name or link

 NotificationCenter.default.removeObserver(self, name: "TimerUpdate", object: nil) 

if you announced your notification at the top of your class, you can directly pass the link of your notification, which will be deleted in your case notification

  NotificationCenter.default.removeObserver(notification) 
+1
source share

You must save the newly added observer in an opaque object ( NSObjectProtocol ) and then call NotificationCenter.default.removeObserver(self.nameOfObserver)

0
source share

All Articles