0
votes

I am using swift 5 on Xcode 11.0, trying to pass a userInfo from view-controller-A to view-controller-B and I already made a notification center post in view-controller-A and here is the code:

 let uid = response["session-id"] as? String

                NotificationCenter.default.post(
                    name: NSNotification.Name("didReceiveSession"),
                    object: nil,
                    userInfo: ["uid" : uid!]
                )

And in view-controller-B I registered the notification and added an observer like so:

  NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.getSessionId),
            name: NSNotification.Name("didReceiveSession"),
            object: nil
        )

And here is getSessionId:

 @objc func getSessionId(_ notification: Notification)
    {
        if let data = notification.userInfo as? [String: String]
        {
            self.session_Id = data["uid"]
        }
    }

I tried a lot of workarounds like:

1- Change name: NSNotification.Name("didReceiveSession") to name: NSNotification.Name(rawValue: "didReceiveSession") in both view controllers

2- Create an instance in view-controller-A:

2.1- share = view-controller-A()

2.2- Change object: nil to object: view-controller-A.share in the addObserver (view-controller-B)

3- Change the selector in view-controller-B from selector: #selector(self.getSessionId) to selector: #selector(getSessionId(_:))

But unfortunately, all attempts did not work. I can't figure out what's wrong.

2
How do you know it's not being called? Set a breakpoint on the if let ... in your handler function. Does that trigger? Also, set breakpoints on both the post and the addObserver call. Is addObservertriggered before post? - Gereon
@Gereon I did set a breakpoint on the handler function and that's how I know it's not being called. I also set a breakpoint on both the post and addObserver, the post is being triggered before the addObserver so nothing suspicious in that. - S.Hasan
On the contrary. If post is triggered before addObserver, who observes the notification? - Gereon
So addObserver should trigger before the post? - S.Hasan
Absolutely. If it doesn't, there is no place for the OS to deliver the notifiaction to, and it is immediately discarded. - Gereon

2 Answers

0
votes

This is a very incorrect way to use Notifications.

What you want to do is pass your "sessionID" value to from VC-A to VC-B when VC-A is moving to / presenting VC-B.

Assuming you are using a segue...

Based on what you posted, you already have a variable / property in VC-B, probably:

var session_ID: [String] = ""

So, in VC-A, you would do something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let nextVC = segue.destination as? VCB {
        nextVC.session_ID = self.uid
    }
}
0
votes

Your problem lies in the sequence of events.

When post is called before addObserver, there is no place for the OS to deliver the notification to, and therefore the notification is discarded.

You need to make sure that you have an active observers, i.e. that addObserver is called before you do your `post.