NotifcationCenter causing a strong reference cycle - Swift 5 - swift

It appears I am getting a Strong Reference Cycle when using the NotifcationCenter.
I am using NotificationCenter to observe the Rotation of the device. (While some would argue this is not the best way to determine the device rotation, this currently seems to be my only route, as no autolayout is being used and storyboard is not being used).
deinit {} is never being called in my ViewController even if I remove the observer in viewWillDisappear and viewDidDisappear.
import UIKit
class TestVC: UIViewController {
deinit {
print("TestClass Deinit") //not being triggered ever
}
#objc private func rotationDetected(sender: Any) {
print("we rotated")
}
override func viewDidDisappear(_ animated: Bool) {
//NotificationCenter.default.removeObserver(UIDevice.orientationDidChangeNotification)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(UIDevice.orientationDidChangeNotification)
//NotificationCenter.default.removeObserver(self) //also doesn't work
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: rotationDetected)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Any ideas as to why this is occurring and how to resolve it?
Also open to any new ideas on how to determine rotation detection else ways (no autolayout or storyboard is being used though).
To reach TestVC() I used self.navigationController?.pushViewController(TestVC(), animated: true) in the previous ViewController and to go back I use the pop.
Without the Observer present, the class will correctly deinit.
RESOLVED
Thanks to the answer marked below the Strong Reference Cycle is removed.
Simply replace NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: rotationDetected)
with
NotificationCenter.default.addObserver(self, selector: #selector(rotationDetected), name: UIDevice.orientationDidChangeNotification, object: nil)

This should work in viewWillDisappear:
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
in combination with using the following in viewWillAppear:
NotificationCenter.default.addObserver(self, selector: #selector(rotationDetected), name: UIDevice.orientationDidChangeNotification, object: nil)

your method of removing observer is incorrect, you should do like this:
class TestVC {
private var observer: Any
func viewWillAppear() {
observer = NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification, object: nil, queue: .main, using: rotationDetected)
}
func viewWillDisappear() {
NotificationCenter.default.removeObserver(observer)
}
}
to eliminate the Strong Reference Cycle, use weak in the closure.

Related

NotificationCenter - addObserver not called

I am trying a very simple code with NotificationCenter. But the addObserver is not getting called. Can any one of you check and let me know what i am missing. There are 2 simple class, one which post notification and another which listens to it. When i run the program, i just see "sending notification" in the console.
Thanks in advance.
Class 1:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("sending notification")
NotificationCenter.default.post(name: Notification.Name("test"), object: nil)
}
}
Class 2:
class secondvc: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("second vc")
NotificationCenter.default.addObserver(self,
selector: #selector(doThisWhenNotify(_:)),
name: Notification.Name("test"),
object: nil)
}
#objc func doThisWhenNotify(_ notification: Notification) {
print("inside notification")
}
}
If, at the time ViewController comes into existence, secondvc does not yet exist, then there is no one there to receive the posted notification and that is why you don't see the notification being received later when secondvc does come into existence.

Add observer without using selector

I need to add observes in my project, but since the manager that I work with, doesn't let me to use #objc in the functions, is there anyway that I can use this function without using #objc?
func createObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(self.updatedata(notification:)),
name: Notification.Name(rawValue: updateNotificationKey), object: nil)
}
#objc dynamic func updatedata(notification: NSNotification) {
updateDataIcon()
}
Your help will be appreciated.
You can use this with the inline block
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: updateNotificationKey) , object: nil, queue: .main) { [weak self] notification in
// to do
}

NsNotificationCenter is not working

Actually i'm new in swift i got stuck here,Anyone can solve this Problem. actOnSpecialNotification Func is not calling on fireNotification in ViewController.swift
In ViewController.swift
func fireNotification() -> Void {
NotificationCenter.default.addObserver(self, selector:
#selector(vikas.updateNotificationSentLabel), name:
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)
}
func updateNotificationSentLabel() {
print("sent")
}
in SecondVC.swift
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:
#selector(ViewController.actOnSpecialNotification), name:
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)
}
func actOnSpecialNotification() {
print("listen")
}
First of all add Observer to your FirstViewConroller.
FirstViewConroller
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:”test”, object: nil)
Now, add the relevant selector method in same ViewController which will be called once the notification will be fired.
func methodOfReceivedNotification(notification: Notification){
//Take Action on Notification
}
Now, you can fire the notification using below lines which will call the above method which resides in FirstViewController
SecondViewController
NSNotificationCenter.defaultCenter().postNotificationName(“test”, object: nil)

ios swift 3 addObserver how to redirect to a certain view from anywhere in the app when push notification tapped

I have an application which has tabBar with 5 tabs
first one let's call it "FirstView" - has following code:
override func viewWillAppear(_ animated: Bool) {
...
NotificationCenter.default.addObserver(self, selector: #selector(self.catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
...
NotificationCenter.default.removeObserver(self)
}
override func viewDidAppear(_ animated: Bool) {
...
NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil, userInfo: userInfo as [AnyHashable: Any])
}
}
I also have code in didReceiveRemoteNotification which ha this bit of code:
NotificationCenter.default.post(name: Notification.Name(rawValue: "myNotif"), object: nil, userInfo: userInfo as [AnyHashable: Any])
And I got UIViewController extension where I have catchIt() and defining redirections logic:
func catchIt(_ userInfo: Notification) {
let notification = userInfo.userInfo
if (path == "account.balance") {
let vc: ProfileViewController = storyboard!.instantiateViewController(withIdentifier: "account.balance") as! ProfileViewController
self.navigationController?.pushViewController(vc, animated: true)
//..and so on for other paths
}
}
This code has 2 problems:
1) Redirection only works if the last opened View was FirstView. if it is other View then I will never get redirected when taping push notification
2) I got my history when it is redirected but it tracks all the way down till the FirstView even if the destination controller is on the other tab different from the FirstViews (I tried to redirect first to required tab and then to destination controller but still it gives the track from the FirstView's tab)
I also tried to use didReceiveRemoteNotification - wrote all the redirections logic into it but it did not work
Would highly appreciate your advice
The problem here, is that the post notification is called before the addObserver. That´s why nothing triggers.
SOLUTION :
Do not addObserver in your viewWillAppear but do it in the init method like this :
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
//Redirect for push notification
NotificationCenter.default.addObserver(self, selector: #selector(self. catchIt), name: NSNotification.Name(rawValue: "myNotif"), object: nil)
}

How is that possible to post notification after 1 second delay or in a dispatch asyn?

i just want to call a method after loading my view. i have set notification to one view and postnotification to other view you will get more idea from my code.. here is my code..
in a one.swift file
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "InsertNote:", name: "insert", object: nil)
}
func InsertNote(notification:NSNotification)
{
println("blablalbla")
}
in a second.swift file
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getData"), userInfo: nil, repeats: false)
}
func getData()
{
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
i have try also in a dispatch_asynch but still it is not work.
Try this
let delayInSeconds: Double = 1
override func viewDidLoad() {
super.viewDidLoad()
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue()){
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
}
The only reason I can come up with that this doesn't work, is that your first ViewController A is already deallocated when you send the notification with the second ViewController B. So, you are sending the notification with B correctly, but A isn't around to receive it.
You can test this by adding an observer in B as well.
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getData"), userInfo: nil, repeats: false)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "insertNote:", name: "insert", object: nil)
}
func getData() {
NSNotificationCenter.defaultCenter().postNotificationName("insert", object: nil)
}
func insertNote:(note: NSNotification) {
print("Banana")
}
// Should print 'Banana' after one second.