iosswiftrx-swiftpublishsubject

Why isn't my rx subscription triggered on my onNext?


Playing around with Rx Swift I have run into a situation where my subscription doesn't trigger.

I have two viewControllers. The first has a label that the subscriber should update, like this:

func listen() {
   print("In func")
   let sec = storyboard?.instantiateViewController(withIdentifier: "secondvc") as! SecondViewController

   sec.myRx.subscribe(onNext: {
    print("SUBSCRIBED", $0)
       self.rxLabel.text = $0
   })
}

If you go to the sencond viewController there is a button that sets off an onNext event. Like this:

    var myRx = PublishSubject<String>()

    @IBAction func myButton(_ sender: Any) {
        myRx.asObserver().onNext("Hello")
    }

So, in my head, when myButton is pressed in the second viewController the label in the first viewController should update when going back to that viewController. But from what I can tell, the function is triggered, but the subscription isn't triggered at all.


Solution

  • Please make sure you are subscribing to the same PublishSubject you're posting events to. Easiest way to confirm this is to by setting breakpoints and checking the address.