1

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.

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18
  • 1
    Are you certain that you are presenting the same view controller that you are instantiating? I ask because that part is not in the snippet. – nanibir Jan 08 '20 at 13:56
  • @nanibir You were right. I changed it, but it still didn't effect the outcome. Thanks for noticing though. Edited answer. – Joakim Sjöstedt Jan 08 '20 at 14:28
  • 1
    @nanibir Ok, solved it now. You were right. It was an issue with not using the same instance on the subscriber as the one used to present the second vc. I made a new instannce and subscribed to that. Thanks for the help. If you want me to upvote and check answer, write a proper response :) – Joakim Sjöstedt Jan 08 '20 at 15:06

1 Answers1

2

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.

nanibir
  • 234
  • 1
  • 9