0

I have a tabBarController (with in my case 4 tabBarViewControllers). on three of these I have Firebase observers which I load in viewWillAppear and remove in viewDidDisappear(ref.removeAllObservers()).

I add them in the next view, but somehow they don't register for some of the observers (in particular .ChildChanged).

If I move to a modally presented viewController (say settings) and move back, the observer works. If I move from a tabBarViewController, where I remove the observers (in viewDidDisappear) and then add them (in viewWillAppear) in the new tabBarViewController, they do not register.

On top of that if I move completely out of the tabBarController (from a view where I remove the observers completely) to a separate viewController via push segue (in my case to a JSQMessagesViewController), the observers that where present in the tabBarViewController do also not register in the new viewController (which is NOT part of the tabBarViewController).

I have read the piece on Best practices for the iOS UIViewController and Firebase (https://www.firebase.com/blog/2015-10-15-best-practices-uiviewcontroller-ios-firebase.html and the post Firebase: when to call removeObserverWithHandle in swift), but somehow that does not seem to apply to tabBarControllers.

My guess is that a tabBarController with various tabBarViewControllers is somehow seen as a single viewController. And that viewControllers pushed on top of that are seen as part of that single viewController as well (in some instances).

Therefore I would like to know: what are best practices in terms of adding and removing observers in a tabBarViewController?

Community
  • 1
  • 1
Peter de Vries
  • 780
  • 1
  • 6
  • 14

1 Answers1

1

Just in case anyone is wondering, I did manage to sort it:

Created a custom TabBarController.swift file (subclassed UITabBarController)

There within viewWillAppear I created the observer, removed at viewDidDisappear.

Now this observer registers changes throughout al of the tabs. it removes the observer when moving to another viewController (via a modally presented viewController). And bring the observer back to live when coming back to any of the tabs.

It still does not do so via a push segue. I solved this by setting a removeAllObservers() just before I call the (push) segue. (once there, I created the same observer again, for sound notification on other chatNodes). (I did however have to create the same observer in that tab, to activate once more when moved back from that childViewController.)

So a tabBarController with various tabBarViewControllers is indeed seen as a single viewController.

Peter de Vries
  • 780
  • 1
  • 6
  • 14