Search code examples
iosswiftfirebasefirebase-realtime-databaseobservers

FirebaseDatabase - removeAllObservers() - What does it actually remove?


Does the removeAllObservers() removes all observers only for DatabaseReference instance it's called on, or any reference at specified path?

For example:

MainController.swift

let ref = Database.(...).child("foo/bar").observe(.value, with: handler)



ItemController.swift

public var ref2: DatabaseReference?

public func someMethod() {
    self.ref2 = Database.(...).child("foo/bar").observe(.value, with: handler2)
}

override func viewDidDisappear() {
    self.ref2?.removeAllObservers()
}

Will ref from MainController still observe foo/bar path or this observer will be removed as well?


Solution

  • According to the documentation:

    If multiple listeners have been added to a database reference, each listener is called when an event is raised. In order to stop syncing data at that location, you must remove all observers at a location by calling the removeAllObservers method.

    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.

    So in other words you need to remove them all, they will no automatically be removed and ref from MainController will still observe.