0

I'm trying to use Rxswift in an application embedded in Tabbar Controller.

I have three view controllers in the tabbar. There are few subscriptions in all view controllers. But for the sake of simplicity lets say first and second view controllers subscribe to the third view controller to get some values.

I get subscribed to the third view controller as coded;

    func startSubscription() {
    let settingsNavigationVC = self.tabBarController?.viewControllers![2] as! UINavigationController
    let settingsVC = settingsNavigationVC.topViewController as! SettingsViewController
    settingsVC.selectedUser.subscribe(onNext: { [weak self] user in
        self?.userToShow = user
    }).disposed(by: disposeBagForFirst)
    }

This works fine if all the tabs are tapped before any data manipulation so all the subscription can start and all app works fine. But if a user directly taps to third vc after loading the app, and makes a change. Then the data at first vc gets updated since it is already loaded when the app started, but the data at second vc doesn't change since there is no subscription yet because the view was not loaded before the changes made in third vc.

I tried to do it with instantiate vc method but that doesn't work either, because with tab bar controllers there is no instantiating views more than once I suppose.

My question is; how can I make sure all the views get loaded before the user interacts with the app, or how should I change my subscription method in order to make it work regardless if all the tabs clicked(views loaded).

Thanks in advance.

emrepun
  • 2,496
  • 2
  • 15
  • 33

1 Answers1

0

In this solution, I load all the views from the first vc's viewDidLoad() method. If you have any idea about how we can modify subscription technique so that we would not need to load all the views, please provide an answer.

I've found the solution thanks to How to load all views in UITabBarController?

It's slightly different since I use navigation controllers. I call this method, at the end of viewDidLoad() after all the configurations, data fetch and everything are completed.

    func loadAllViewsOfTabbarController() {
        if let viewControllers = tabBarController?.viewControllers {
            for viewController in viewControllers {
                let viewInNav = viewController as! UINavigationController
                let _ = viewInNav.topViewController?.view!
            }
        }
    }
emrepun
  • 2,496
  • 2
  • 15
  • 33