Search code examples
iosswiftfirebasefirebase-realtime-databaseobservers

My Firebase Observers will not stop Observing


In my:

override func viewWillAppear(_ animated: Bool) 

I create my Firebase observers as such:

poolsRef.child(pID).child("lock").observe(.value, with: { snapshot in

boxesRef.child(pID).observe(.value, with: { snapshot in

coordRef.child(pID).child("x").observe(.value, with: { snapshot in

coordRef.child(pID).child("y").observe(.value, with: { snapshot in

poolsRef.child(pID).child("Winner").observe(.value, with: { snapshot in

playersRef.child("pID").observe(.value, with: { snapshot in

then in

override func viewDidDisappear(_ animated: Bool)

I call the removeAllObservers:

coordRef.removeAllObservers()

poolsRef.removeAllObservers()

boxesRef.removeAllObservers()

playersRef.removeAllObservers()

The problem is when I leave the view where all the observers were created and even thought the removeAllObservers() is called in the viewDidDisappear I am no a different view altogether and if I go to the Firebase directly and make a change in regards to data that was related to the observers mentioned above, I can see that the app is still listening when it shouldn't!?

How do I resolve this issue!!


Solution

  • From Docs

    Calling removeObserverWithHandle or removeAllObservers on a listener does not automatically remove listeners registered on its child nodes; you must also keep track of those references or handles to remove them.

    poolsRef.child(pID).child("lock").removeAllObservers()
    poolsRef.child(pID).child("Winner").removeAllObservers()
    

    and so on with other observers